fix(actualgraph): fix chart
This commit is contained in:
parent
47b90ec2ca
commit
40b0e21e18
|
|
@ -51,7 +51,23 @@ export class ActualgraphComponent implements OnInit {
|
||||||
{ x: npkData.soilpotassium ?? 0, y: 2 }
|
{ x: npkData.soilpotassium ?? 0, y: 2 }
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Data untuk standar (contoh: Start dan End Range)
|
||||||
|
const startDataRange = [
|
||||||
|
{ x: 100, y: 0 },
|
||||||
|
{ x: 90, y: 1 },
|
||||||
|
{ x: 220, y: 2 }
|
||||||
|
];
|
||||||
|
|
||||||
|
const endDataRange = [
|
||||||
|
{ x: 100, y: 0 },
|
||||||
|
{ x: 35, y: 1 },
|
||||||
|
{ x: 200, y: 2 }
|
||||||
|
];
|
||||||
|
|
||||||
this.chartData.datasets[0].data = actualData;
|
this.chartData.datasets[0].data = actualData;
|
||||||
|
this.chartData.datasets[1].data = startDataRange;
|
||||||
|
this.chartData.datasets[2].data = endDataRange;
|
||||||
|
|
||||||
this.isLoading = false;
|
this.isLoading = false;
|
||||||
this.cdr.detectChanges();
|
this.cdr.detectChanges();
|
||||||
|
|
||||||
|
|
@ -82,8 +98,12 @@ export class ActualgraphComponent implements OnInit {
|
||||||
if (this.chartCanvas?.nativeElement) {
|
if (this.chartCanvas?.nativeElement) {
|
||||||
const ctx = this.chartCanvas.nativeElement.getContext('2d');
|
const ctx = this.chartCanvas.nativeElement.getContext('2d');
|
||||||
if (ctx) {
|
if (ctx) {
|
||||||
|
// If chart already exists, destroy it before creating a new one
|
||||||
|
if (this.chart) {
|
||||||
|
this.chart.destroy();
|
||||||
|
}
|
||||||
this.chart = new Chart(ctx, {
|
this.chart = new Chart(ctx, {
|
||||||
type: 'bar',
|
type: 'bubble',
|
||||||
data: this.chartData,
|
data: this.chartData,
|
||||||
options: this.chartOptions
|
options: this.chartOptions
|
||||||
});
|
});
|
||||||
|
|
@ -103,7 +123,7 @@ export class ActualgraphComponent implements OnInit {
|
||||||
datasets: [
|
datasets: [
|
||||||
{
|
{
|
||||||
label: 'Actual Data',
|
label: 'Actual Data',
|
||||||
data: [],
|
data: [] as { x: number; y: number; }[],
|
||||||
backgroundColor: 'rgb(18, 55, 42)',
|
backgroundColor: 'rgb(18, 55, 42)',
|
||||||
borderColor: 'rgb(18, 55, 42)',
|
borderColor: 'rgb(18, 55, 42)',
|
||||||
borderWidth: 1,
|
borderWidth: 1,
|
||||||
|
|
@ -111,23 +131,15 @@ export class ActualgraphComponent implements OnInit {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Start Data Range',
|
label: 'Start Data Range',
|
||||||
data: [
|
data: [],
|
||||||
{ x: 100, y: 0 },
|
|
||||||
{ x: 90, y: 1 },
|
|
||||||
{ x: 220, y: 2 }
|
|
||||||
],
|
|
||||||
backgroundColor: 'rgba(0,0,0,0)',
|
backgroundColor: 'rgba(0,0,0,0)',
|
||||||
borderWidth: 0,
|
borderWidth: 0,
|
||||||
type: 'bar',
|
type: 'bar',
|
||||||
stack: 'range',
|
stack: 'range',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Standard Data Range',
|
label: 'End Data Range',
|
||||||
data: [
|
data: [],
|
||||||
{ x: 100, y: 0 },
|
|
||||||
{ x: 35, y: 1 },
|
|
||||||
{ x: 200, y: 2 }
|
|
||||||
],
|
|
||||||
backgroundColor: 'rgb(212, 231, 197)',
|
backgroundColor: 'rgb(212, 231, 197)',
|
||||||
borderWidth: 1,
|
borderWidth: 1,
|
||||||
type: 'bar',
|
type: 'bar',
|
||||||
|
|
@ -136,10 +148,10 @@ export class ActualgraphComponent implements OnInit {
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
public chartOptions = {
|
public chartOptions: Partial<Chart['options']> = {
|
||||||
responsive: true,
|
responsive: true,
|
||||||
maintainAspectRatio: false,
|
maintainAspectRatio: false,
|
||||||
indexAxis: 'y',
|
indexAxis: 'y' as const,
|
||||||
scales: {
|
scales: {
|
||||||
x: {
|
x: {
|
||||||
title: {
|
title: {
|
||||||
|
|
@ -156,41 +168,26 @@ export class ActualgraphComponent implements OnInit {
|
||||||
text: 'Sensor Parameter',
|
text: 'Sensor Parameter',
|
||||||
},
|
},
|
||||||
stacked: true,
|
stacked: true,
|
||||||
|
ticks: {
|
||||||
|
display: true,
|
||||||
|
callback: function (value: number) {
|
||||||
|
const labels = ['Nitrogen', 'Phosphorus', 'Kalium'];
|
||||||
|
return labels[value as number];
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
plugins: {
|
plugins: {
|
||||||
tooltip: {
|
tooltip: {
|
||||||
callbacks: {
|
callbacks: {
|
||||||
title: function (tooltipItem: any) {
|
|
||||||
return tooltipItem[0].label;
|
|
||||||
},
|
|
||||||
label: function (tooltipItem: any) {
|
label: function (tooltipItem: any) {
|
||||||
const dataset = tooltipItem.dataset;
|
if (tooltipItem.datasetIndex === 0) {
|
||||||
const dataIndex = tooltipItem.dataIndex;
|
return `Value: ${tooltipItem.raw.x} mg/L`;
|
||||||
const data = dataset.data[dataIndex];
|
|
||||||
|
|
||||||
let tooltipText = `Actual Value: ${data.x} mg/L`;
|
|
||||||
|
|
||||||
if (dataset.type === 'bar') {
|
|
||||||
const startRangeDataset = tooltipItem.chart.data.datasets.find((ds: any) => ds.label === 'Start Data Range');
|
|
||||||
const standardRangeDataset = tooltipItem.chart.data.datasets.find((ds: any) => ds.label === 'Standard Data Range');
|
|
||||||
|
|
||||||
const startRangeValue = startRangeDataset?.data.find((point: any) => point.y === data.y)?.x;
|
|
||||||
const standardRangeValue = standardRangeDataset?.data.find((point: any) => point.y === data.y)?.x;
|
|
||||||
|
|
||||||
if (startRangeValue !== undefined && standardRangeValue !== undefined) {
|
|
||||||
const minValue = startRangeValue;
|
|
||||||
const maxValue = startRangeValue + standardRangeValue;
|
|
||||||
tooltipText = `(Standard Range: ${minValue} - ${maxValue} mg/L)`;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return '';
|
||||||
return tooltipText;
|
}
|
||||||
},
|
}
|
||||||
},
|
}
|
||||||
},
|
}
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user