gpt4 book ai didi

javascript - Chart.js,虚线,全宽图表

转载 作者:行者123 更新时间:2023-12-03 21:37:51 27 4
gpt4 key购买 nike

是否可以在 Chart.js 折线图上制作虚线?并使图表全宽?见附件样机。

enter image description here

这是我当前的代码(只是简单的例子):

    var ctx = document.getElementById("main-line-chart").getContext("2d");
var line = ctx.setLineDash([5, 15]);

var gradient = ctx.createLinearGradient(0, 0, 0, 300);
gradient.addColorStop(0, 'rgba(40,175,250,.25)');
gradient.addColorStop(1, 'rgba(40,175,250,0)');

var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
pointDot : false,
fillColor: gradient,
strokeColor: "#28AFFA",
pointColor: "#19283F",
pointStrokeColor: "#28AFFA",
pointHighlightFill: "#19283F",
pointHighlightStroke: "#28AFFA",
data: [65, 59, 80, 81, 56, null, null]
},
{
label: "My Second dataset",
fillColor: "rgba(0,0,0,0)",
strokeColor: "rgba(255,255,255,.39)",
pointColor: "#19283F",
pointStrokeColor: "rgba(255,255,255,.39)",
pointHighlightFill: "#19283F",
pointHighlightStroke: "#28AFFA",
data: [null, null, null, null, 56, 27, 90]
}
]
};

var options = {};

var myLineChart = new Chart(ctx).Line(data, options);
console.log(myLineChart);

最佳答案

自发布此问题以来,Chart.js 已经发展,但即使使用当前版本 2.9.4,该解决方案看起来与最初发布的代码几乎相同。
主要变化如下:

  • 而不是使用 new Chart(ctx).Line(...) ,我们现在应该使用 type: 'line'如图here .
  • 不少 dataset properties有不同的名称。
  • 使用选项 borderDash 绘制第二个数据集的虚线。

  • 请查看您修改后的代码,看看它是如何工作的。

    var ctx = document.getElementById("main-line-chart").getContext("2d");
    var gradient = ctx.createLinearGradient(0, 0, 0, 300);
    gradient.addColorStop(0, 'rgba(40,175,250,.25)');
    gradient.addColorStop(1, 'rgba(40,175,250,0)');

    var data = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
    label: "My First dataset",
    backgroundColor: gradient,
    borderColor: "#28AFFA",
    pointColor: "#19283F",
    pointHighlightColor: "#28AFFA",
    data: [65, 59, 80, 81, 56, null, null]
    },
    {
    label: "My Second dataset",
    fill: false,
    borderColor: "rgba(100, 100, 100,.39)",
    pointColor: "#19283F",
    pointHighlightColor: "#28AFFA",
    data: [null, null, null, null, 56, 27, 90],
    borderDash: [10, 5]
    }
    ]
    };

    var options = {};
    var myLineChart = new Chart(ctx, {
    type: "line",
    data: data,
    options: options
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
    <canvas id="main-line-chart" height="100"></canvas>

    关于javascript - Chart.js,虚线,全宽图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32920301/

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