Chart.js 混合图表的 OnClick 事件,我点击了哪个图表?
Chart.js OnClick event with a mixed chart, which chart did I click?
编辑:修改以添加选项,以及建议的(来自答案)chartClickEvent,这是一个 jsfiddle:http://jsfiddle.net/jmpxgufu/174/
想象一下,如果您要 Chart.js 具有以下配置的混合图表:
var config = {
type: 'bar',
data: {
labels: ["Test","Test","Test"],
datasets: [{
label: 'Dataset1',
yAxisID: 'Dataset1',
type: "line",
borderColor: "red",
backgroundColor: "red",
data: [70,60,50],
fill: false
},
{
label: 'Dataset0',
type: "bar",
backgroundColor: "blue",
data: [100,90,80]
}]
},
options: {
scales: {
xAxes: [{ barPercentage: 1.0 }],
yAxes: [{ id: 'Dataset1', position: 'left', type: 'linear',
ticks: { display: false, min: 0, beginAtZero: true, max: 120 },
scaleLabel: { display: true, labelString: "TestScale" } }]
},
responsive: true,
maintainAspectRatio: false,
legend : { display: true, position: 'bottom' },
onClick: chartClickEvent
}
}; // end of var config
function chartClickEvent(event, array)
{
if (window.myChart === undefined || window.myChart == null)
{
return;
}
if (event === undefined || event == null)
{
return;
}
if (array === undefined || array == null)
{
return;
}
if (array.length <= 0)
{
return;
}
var active = window.myChart.getElementAtEvent(event);
if (active === undefined || active == null)
{
return;
}
var elementIndex = active[0]._datasetIndex;
console.log("elementIndex: " + elementIndex + "; array length: " + array.length);
if (array[elementIndex] === undefined || array[elementIndex] == null)
{
return;
}
var chartData = array[elementIndex]['_chart'].config.data;
var idx = array[elementIndex]['_index'];
var label = chartData.labels[idx];
var value = chartData.datasets[elementIndex].data[idx];
var series = chartData.datasets[elementIndex].label;
alert(series + ':' + label + ':' + value);
}
正如我的 chartClickEvent 所说,我的数组长度为 2,因为我有两个图表。太好了,但是我不知道如何确定是使用数组 [0] 还是数组 [1]。如果他们专门单击线数据点,我想对该数据 (array[0]) 做一些事情,如果他们单击大蓝色条,我想对该数据 (array[1]) 做一些事情。我如何判断他们点击的是直线还是栏?
谢谢。
HTML
<div id="test" style="height:600px; width:600px;">
<canvas id="myCanvas" style="border: 1px solid black; margin: 25px 25px, display: none;" height="300" >Canvas</canvas>
</div>
JS
var ctx = document.getElementById("myCanvas");
var newArr;
var config = new Chart(ctx,{
type: 'bar',
data: {
labels: ["Test","Test","Test"],
datasets: [{
label: 'Dataset1',
yAxisID: 'Dataset1',
type: "line",
borderColor: "red",
backgroundColor: "red",
data: [70,60,50],
fill: false
},
{
label: 'Dataset0',
type: "bar",
backgroundColor: "blue",
data: [100,90,80]
}]
},
options: {
scales: {
xAxes: [{ barPercentage: 1.0 }],
yAxes: [{ id: 'Dataset1', position: 'left', type: 'linear',
ticks: { display: false, min: 0, beginAtZero: true, max: 120 },
scaleLabel: { display: true, labelString: "TestScale" } }]
},
responsive: true,
maintainAspectRatio: false,
legend : { display: true, position: 'bottom' },
onClick: chartClickEvent
}
}); // end of var config
function chartClickEvent(event, array){
if(typeof newArr === 'undefined'){
newArr = array;
}
if (window.config === 'undefined' || window.config == null)
{
return;
}
if (event === 'undefined' || event == null)
{
return;
}
if (newArr === 'undefined' || newArr == null)
{
return;
}
if (newArr.length <= 0)
{
return;
}
var active = window.config.getElementAtEvent(event);
if (active === 'undefined' || active == null || active.length === 0)
{
return;
}
var elementIndex = active[0]._datasetIndex;
console.log("elementIndex: " + elementIndex + "; array length: " + newArr.length);
if (newArr[elementIndex] === 'undefined' || newArr[elementIndex] == null){
return;
}
var chartData = newArr[elementIndex]['_chart'].config.data;
var idx = newArr[elementIndex]['_index'];
var label = chartData.labels[idx];
var value = chartData.datasets[elementIndex].data[idx];
var series = chartData.datasets[elementIndex].label;
alert(series + ':' + label + ':' + value);
}
编辑:修改以添加选项,以及建议的(来自答案)chartClickEvent,这是一个 jsfiddle:http://jsfiddle.net/jmpxgufu/174/
想象一下,如果您要 Chart.js 具有以下配置的混合图表:
var config = {
type: 'bar',
data: {
labels: ["Test","Test","Test"],
datasets: [{
label: 'Dataset1',
yAxisID: 'Dataset1',
type: "line",
borderColor: "red",
backgroundColor: "red",
data: [70,60,50],
fill: false
},
{
label: 'Dataset0',
type: "bar",
backgroundColor: "blue",
data: [100,90,80]
}]
},
options: {
scales: {
xAxes: [{ barPercentage: 1.0 }],
yAxes: [{ id: 'Dataset1', position: 'left', type: 'linear',
ticks: { display: false, min: 0, beginAtZero: true, max: 120 },
scaleLabel: { display: true, labelString: "TestScale" } }]
},
responsive: true,
maintainAspectRatio: false,
legend : { display: true, position: 'bottom' },
onClick: chartClickEvent
}
}; // end of var config
function chartClickEvent(event, array)
{
if (window.myChart === undefined || window.myChart == null)
{
return;
}
if (event === undefined || event == null)
{
return;
}
if (array === undefined || array == null)
{
return;
}
if (array.length <= 0)
{
return;
}
var active = window.myChart.getElementAtEvent(event);
if (active === undefined || active == null)
{
return;
}
var elementIndex = active[0]._datasetIndex;
console.log("elementIndex: " + elementIndex + "; array length: " + array.length);
if (array[elementIndex] === undefined || array[elementIndex] == null)
{
return;
}
var chartData = array[elementIndex]['_chart'].config.data;
var idx = array[elementIndex]['_index'];
var label = chartData.labels[idx];
var value = chartData.datasets[elementIndex].data[idx];
var series = chartData.datasets[elementIndex].label;
alert(series + ':' + label + ':' + value);
}
正如我的 chartClickEvent 所说,我的数组长度为 2,因为我有两个图表。太好了,但是我不知道如何确定是使用数组 [0] 还是数组 [1]。如果他们专门单击线数据点,我想对该数据 (array[0]) 做一些事情,如果他们单击大蓝色条,我想对该数据 (array[1]) 做一些事情。我如何判断他们点击的是直线还是栏?
谢谢。
HTML
<div id="test" style="height:600px; width:600px;">
<canvas id="myCanvas" style="border: 1px solid black; margin: 25px 25px, display: none;" height="300" >Canvas</canvas>
</div>
JS
var ctx = document.getElementById("myCanvas");
var newArr;
var config = new Chart(ctx,{
type: 'bar',
data: {
labels: ["Test","Test","Test"],
datasets: [{
label: 'Dataset1',
yAxisID: 'Dataset1',
type: "line",
borderColor: "red",
backgroundColor: "red",
data: [70,60,50],
fill: false
},
{
label: 'Dataset0',
type: "bar",
backgroundColor: "blue",
data: [100,90,80]
}]
},
options: {
scales: {
xAxes: [{ barPercentage: 1.0 }],
yAxes: [{ id: 'Dataset1', position: 'left', type: 'linear',
ticks: { display: false, min: 0, beginAtZero: true, max: 120 },
scaleLabel: { display: true, labelString: "TestScale" } }]
},
responsive: true,
maintainAspectRatio: false,
legend : { display: true, position: 'bottom' },
onClick: chartClickEvent
}
}); // end of var config
function chartClickEvent(event, array){
if(typeof newArr === 'undefined'){
newArr = array;
}
if (window.config === 'undefined' || window.config == null)
{
return;
}
if (event === 'undefined' || event == null)
{
return;
}
if (newArr === 'undefined' || newArr == null)
{
return;
}
if (newArr.length <= 0)
{
return;
}
var active = window.config.getElementAtEvent(event);
if (active === 'undefined' || active == null || active.length === 0)
{
return;
}
var elementIndex = active[0]._datasetIndex;
console.log("elementIndex: " + elementIndex + "; array length: " + newArr.length);
if (newArr[elementIndex] === 'undefined' || newArr[elementIndex] == null){
return;
}
var chartData = newArr[elementIndex]['_chart'].config.data;
var idx = newArr[elementIndex]['_index'];
var label = chartData.labels[idx];
var value = chartData.datasets[elementIndex].data[idx];
var series = chartData.datasets[elementIndex].label;
alert(series + ':' + label + ':' + value);
}