gpt4 book ai didi

javascript - 单击折线图中的单个点但不单击条形图中的单个条时显示 Bootstrap 模式

转载 作者:太空狗 更新时间:2023-10-29 14:17:29 25 4
gpt4 key购买 nike

我试图在单击图表 js 中的各个栏而不是工具提示时显示 Bootstrap 模式。

我编写了用于在单击折线图中的特定 x 值时显示 Bootstrap 模式的代码。但是当我将 linecchart 更改为具有相同数据集的条形图时,它不适用于条形图。据我所知,差异黑白线图和条形图是图形表示和可视化。如有不妥请指正。

图像文件:

Screenshot of output which i explained above

HTML:

<div class="chart1">
<canvas id="chart" width="300" height="150"></canvas>
</div>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
You clicked in June
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>

<div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
You clicked in May
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>

JavaScript :

  <script type="text/javascript">

var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
}, {
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: [28, 48, 40, 19, 86, 27, 90]
}]
};

var canvas = document.getElementById("chart");
var ctx = canvas.getContext("2d");
var chart = new Chart(ctx).Line(data, {
responsive: true
});


canvas.onclick = function(evt) {
var points = chart.getPointsAtEvent(evt);
var value = chart.datasets[0].points.indexOf(points[0]);
if(value == 5){
$('#myModal').modal('show');
} else if(value == 4){
$('#myModal1').modal('show');
}


};

</script>

此代码在 Linechart 中工作得很好,但我想在 Barchart 中使用相同的代码,但我试过但没有得到输出。

最佳答案

使用 getBarsAtEvent 而不是 getPointsAtEvent

关于javascript - 单击折线图中的单个点但不单击条形图中的单个条时显示 Bootstrap 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35100003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com