I'm using a scatter chart of ChatJS which look like this:
I'm trying to add the data values to be shown inside the graph (so for example the above line, middle point should show the label ~0.05)
I tried to follow this thread, but i think scatter chart acts different than line/bar charts, thus its not working for me.
My code:
function createStatisticalResultsChart(kpiData) {
var kpiName = kpiData.kpi_name;
var kpiChartId = `${kpiName}-chart`;
if (kpiData.x_axes_dict.g2_values) {
var scatterChartDatasets = generateScatterChartDatasets(kpiData, num_test_groups);
new Chart(document.getElementById(kpiChartId), {
type: 'scatter',
data: {
datasets: scatterChartDatasets
},
options: {
legend: false,
tooltips: false,
scales: {
y: {
display: false,
suggestedMin: -2,
suggestedMax: 2,
},
x: {
suggestedMin: -1,
suggestedMax: 1,
ticks: {
// Include a dollar sign in the ticks
callback: function(value, index, values) {
return value + '%';
}
}
}
}
}
})
} else {
// If not xAxisList - it means the upper & lower bound returned NULL results from memdsql
var canvasObject = document.getElementById(kpiChartId);
var ctx = canvasObject.getContext("2d");
ctx.font = "12px Verdana";
ctx.fillText("Lower & Upper bounds data is N/A", 10, 90);
}
};
function generateScatterChartDatasets(kpiData, num_test_groups) {
var xAxisValuesG2 = kpiData.x_axes_dict.g2_values;
// Y value of 0 (straight line)
var finalXAxisG2Array = generateScatterChartData(xAxisValuesG2, 1);
var groupB_Dataset = {
label: 'Group-B',
data: finalXAxisG2Array,
borderColor: `${significance_colors_dict[kpiData.uplift_g2_significant_level]}`,
backgroundColor: 'white',
showLine: true,
pointStyle: 'cross',
pointRadius: 10,
};
var finalDatasets = [];
finalDatasets.push(groupB_Dataset);
if (num_test_groups > 2) {
var xAxisValuesG3 = kpiData.x_axes_dict.g3_values;
// Y value of 0 (straight line)
var finalXAxisG3Array = generateScatterChartData(xAxisValuesG3, 0);
var groupC_Dataset = {
label: 'Group-C',
data: finalXAxisG3Array,
borderColor: `${significance_colors_dict[kpiData.uplift_g3_significant_level]}`,
backgroundColor: 'white',
showLine: true,
borderDash: [10, 5],
pointStyle: 'cross',
pointRadius: 10,
};
finalDatasets.push(groupC_Dataset);
}
return finalDatasets;
}
function generateScatterChartData(xAxisValues, yValue) {
// finalArray will match the scatter-chart required data
// yValue should be always a INT CONSTANT (0/1/2)
var finalArray = [];
for (var i = 0; i < xAxisValues.length; i++) {
finalArray.push({
'x': xAxisValues[i] * 100,
'y': yValue
})
}
return finalArray;
}
// MAIN
createStatisticalResultsChart(kpiDataFromDatabase);