Reputation: 199
I am creating a chart in Angular 10 using Chart.js 3.6.2.
Some problems I am struggling with:
My goal is like this image (which someone else created using the older Chart.js 2), preferably with the same number of horizontal gridlines:
I have created a simple logarithmic chart with 3 data points per dataset. Relevant code is shown below and in this Stackblitz. Comments show some different things I have tried. Note that this code has a 2nd dataset, which is commented out. Uncommenting this dataset causes Y-axis changes.
<div style="padding: 1rem">
<canvas id="myChart" width="400" height="400"></canvas>
</div>
export class AppComponent implements OnInit {
@ViewChild('chartRef', { static: true }) chartRef: ElementRef;
chart: Chart;
ngOnInit() {
this.createChart();
}
private get ctx(): CanvasRenderingContext2D {
return (this.chartRef.nativeElement as HTMLCanvasElement).getContext(
'2d'
) as CanvasRenderingContext2D;
}
private createChart(): void {
if (this.chart) {
this.chart.destroy();
}
if (!this.ctx) {
return;
}
const myChart = new Chart(this.ctx, {
type: 'line',
data: {
labels: ['Red', 'Blue', 'Yellow'],
datasets: [
{
label: 'Blue Data',
data: [0, 11.1, 20],
borderColor: '#2184c6',
},
/* MARK COMMENT ONE: Uncommenting this changes the Y-axis */
// {
// label: 'Orange Data',
// data: [0, 0.1, 85],
// borderColor: '#ff6f20',
// },
],
},
options: {
scales: {
y:
/** MARK COMMENT TWO:
Uncommenting array brackets ("[" and "]") will cause non-logarithmic ticks (linear tick behavior), but starts with "0". I want to start with zero, with log ticks.
*/
// [
{
type: 'logarithmic',
bounds: 'ticks',
// NOT A PROP FOR type: "logarithmic"
// beginAtZero: true, //
min: 0,
max: 100, // required to make this always go to 100
// grace: 10,
// suggestedMax: 100,
ticks: {
display: true,
// max: 100,
// min: 20,
// maxTicksLimit: 101,
},
},
// ]
},
},
});
}
}
Upvotes: 0
Views: 917
Reputation: 369
you're data on stackblitz can't start with 0, it needs a data point 0.01 for example - hope this helps
Upvotes: 0
Reputation: 199
Not fixable using chartjs-3. The "solution" is to continue using chartjs-2 for now.
Although these properties/features, which were available in chartjs-2 were removed in chartjs-3, according to bug reports listed below, there appear to be plans to address them in upcoming V4.
Here are links to some related bug reports from the chartjs github repo (for version 3):
Refine logarithmic scaling / tick generation #9166
log scale step size too large #7332
v3 Regression: Line doesn't draw when points are out of range #9564
Upvotes: 0