android 折线图 点击事件,javascript – 如何使用Chart.js将点击事件添加到我的折线图...

我试图使用

Chart.js将点击事件添加到我的折线图中.我已经启用了我的工具提示,以显示我的折线图中的信息,但我也想添加一个点击方式,让我知道用户点击x轴.现在我只想弹出一个警报,给我在用户点击的x轴上的值.

研究:

我看过documentation of Chart.js,我发现这个方法:.getPointsAtEvent(event)

Calling getPointsAtEvent(event) on your Chart instance passing an

argument of an event, or jQuery event, will return the point elements

that are at that the same position of that event.

canvas.onclick = function(evt){

var activePoints = myLineChart.getPointsAtEvent(evt);

// => activePoints is an array of points on the canvas that are at the same position as the click event. };

我不知道如何使用或在哪里将函数放在我的代码中.如果有人可以帮助我找出可以添加到我的代码的地方,那将是非常感谢!

我的代码:(在javascript中)

//NOTE: the div 'roomForChart' has been already declared as

//creating html code inside of javascript to display the canvas used for the graph

htmlForGraph = "";

document.getElementById('roomForChart').innerHTML += htmlForGraph;

//NOW TO CREATE DATA

//the data for my line chart

var data = {

labels: ["Aug 1", "Aug 2", "Aug 3","Aug 4","Aug 5"], //the x axis

datasets: [

{ //my red line

label: "Usage Plan",

fillColor: "rgba(255,255,255,0.2)", //adds the color below the line

strokeColor: "rgba(224,0,0,1)",//creates the line

pointColor: "rgba(244,0,0,1)",

pointStrokeColor: "#fff",

pointHighlightFill: "#fff",

pointHighlightStroke: "rgba(220,220,220,1)",

data: [1024, 1024, 1024, 1024, 1024]

},

{ //my green line

label: "Overall Usage",

fillColor: "rgba(48,197,83,0.2)",

strokeColor: "rgba(48,197,83,1)",

pointColor: "rgba(48,197,83,1)",

pointStrokeColor: "#fff",

pointHighlightFill: "#fff",

pointHighlightStroke: "rgba(48,197,83,1)",

data: [15, 25, 45, 45, 1500]

},

{ //my blue line

label: "Daily Usage",

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: [15, 10, 20, 0, 5]

}

] //ending the datasets

}; //ending data

//creating a variable for my chart

var ctx = document.getElementById("myChart").getContext("2d");

//code to create a maximum y value on the chart

var maxUsage = 1024;

var maxSteps = 5;

var myLineChart = new Chart(ctx).Line(data, {

pointDot: false,

scaleOverride: true,

scaleSteps: maxSteps,

scaleStepWidth: Math.ceil(maxUsage / maxSteps),

scaleStartValue: 0

});

//what I have tried but it doesn't show an alert message

ctx.onclick = function(evt){

var activePoints = myLineChart.getPointsAtEvent(evt);

// => activePoints is an array of points on the canvas that are at the same position as the click event.

alert("See me?");

};

对于那些在这里很难看到图表的人,你去:

希望我提供足够的信息来获得一些帮助.如果我需要自己解释,请让我知道.先谢谢你!!! 🙂

首先,您需要编写一个后端API来从数据库中获取数据。这个API应该返回一个JSON格式的数据。例如,如果您使用PHP语言,可以编写以下代码: ```php <?php // 连接数据库 $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "dbname"; $conn = mysqli_connect($servername, $username, $password, $dbname); // 检查连接 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } // 查询数据库 $sql = "SELECT * FROM mytable"; $result = mysqli_query($conn, $sql); // 处理查询结果 $data = array(); while($row = mysqli_fetch_assoc($result)) { $data[] = $row; } // 返回JSON格式的数据 header('Content-Type: application/json'); echo json_encode($data); // 关闭连接 mysqli_close($conn); ?> ``` 然后,您需要编写一个前端页面来使用Ajax从这个API获取数据并使用Chart.js绘制折线图。以下是一个基本的HTML和JavaScript代码示例: ```html <!DOCTYPE html> <html> <head> <title>Chart.js Example</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script> </head> <body> <canvas id="myChart"></canvas> <script> // 使用Ajax从后端API获取数据 var xhr = new XMLHttpRequest(); xhr.open('GET', 'api.php'); xhr.onload = function() { if (xhr.status === 200) { var data = JSON.parse(xhr.responseText); // 使用Chart.js绘制折线图 var ctx = document.getElementById('myChart').getContext('2d'); var myChart = new Chart(ctx, { type: 'line', data: { labels: data.map(function(item) { return item.date; }), datasets: [{ label: 'My Dataset', data: data.map(function(item) { return item.value; }), fill: false, borderColor: 'rgb(75, 192, 192)', tension: 0.1 }] }, options: { scales: { yAxes: [{ ticks: { beginAtZero: true } }] } } }); } else { console.log('Request failed. Returned status of ' + xhr.status); } }; xhr.send(); </script> </body> </html> ``` 您需要根据自己的实际情况修改这些代码,以适应您的数据库结构和数据格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值