【问题标题】:Styling Bars and Lines with Chart.js使用 Chart.js 设置条形和线条的样式
【发布时间】:2015-12-14 17:38:44
【问题描述】:

我们已经使用 Chart.js 几个月了,并且喜欢它为我们提供的易于编程的强大功能。我们想开始添加到从 Chart.js 生成的图表中的一件事是我们生成的图表的样式更好一点。我们使用的大多数图表都是条形图,也有一些折线图。

当我使用“样式”一词时,我真正在谈论的是使条形或线条看起来更好一点。具体来说,我想在条形图和折线图后面添加一个阴影,甚至可能在条形图上添加一个斜角。

我查看了很多问题,但似乎无法找到我要查找的内容。我还通过修改 Chart.js 文件以向 javascript 添加阴影和模糊进行了一些实验,但我没有将它添加到正确的位置。我将这些更改放在 Chart.Element.extend 绘制函数中:

ctx.shadowColor = '#000';
ctx.shadowBlur = 10;
ctx.shadowOffsetX = 8;
ctx.shadowOffsetY = 8;

我把它放在 ctx.fill() 之前,它几乎可以满足我的要求。结果是我在绘制的条形图和折线图上都得到了一个看起来不错的阴影,但我在 x 和 y 轴的标签上也得到了一个阴影,看起来不太好。我希望只在条形和线条上放置阴影,而不是在标签上。

您能提供的任何帮助将不胜感激。我没有使用 javascript 的经验,但已经能够完成相当多的编码,如果没有 Stack Overflow 上每个人的帮助,我将无法做到。

【问题讨论】:

标签: chart.js dropshadow bevelled


【解决方案1】:

为折线图添加阴影

您可以扩展折线图类型来做到这一点


预览


脚本

Chart.types.Line.extend({
    name: "LineAlt",
    initialize: function () {
        Chart.types.Line.prototype.initialize.apply(this, arguments);

        var ctx = this.chart.ctx;
        var originalStroke = ctx.stroke;
        ctx.stroke = function () {
            ctx.save();
            ctx.shadowColor = '#000';
            ctx.shadowBlur = 10;
            ctx.shadowOffsetX = 8;
            ctx.shadowOffsetY = 8;
            originalStroke.apply(this, arguments)
            ctx.restore();
        }
    }
});

然后

...
var myChart = new Chart(ctx).LineAlt(data, {
    datasetFill: false
});

小提琴 - https://jsfiddle.net/7kbz1L4t/

【讨论】:

  • 土豆皮,谢谢!这不被评为如何让我大吃一惊。在 10 个阴影答案中,这是唯一一个最简单但更重要的是唯一一个真正有效的答案。谢谢!
  • 你也可以扩展饼图吗?
  • 您介意解释一下您对ctx.stroke 做了什么吗?文档说它可以接收 Path2D,但是你传递了一个自定义函数。感谢您的回答!
  • @MiguelPéres - 我用猴子修补了它 - 基本上用我自己的一个替换了实际实现,它在调用我存储在 originalStroke 中的原始实际实现之前做了一些其他事情。
【解决方案2】:

???????? ??? ??????? ?.?.?

ᴘʀᴇᴠɪᴇᴡ


ꜱᴄʀɪᴘᴛ覆盖绘图函数

let draw = Chart.controllers.line.prototype.draw;
Chart.controllers.line.prototype.draw = function() {
    draw.apply(this, arguments);
    let ctx = this.chart.chart.ctx;
    let _stroke = ctx.stroke;
    ctx.stroke = function() {
        ctx.save();
        ctx.shadowColor = '#07C';
        ctx.shadowBlur = 10;
        ctx.shadowOffsetX = 0;
        ctx.shadowOffsetY = 4;
        _stroke.apply(this, arguments);
        ctx.restore();
    }
};

let draw = Chart.controllers.line.prototype.draw;
Chart.controllers.line.prototype.draw = function() {
    draw.apply(this, arguments);
    let ctx = this.chart.chart.ctx;
    let _stroke = ctx.stroke;
    ctx.stroke = function() {
        ctx.save();
        ctx.shadowColor = '#07C';
        ctx.shadowBlur = 10;
        ctx.shadowOffsetX = 0;
        ctx.shadowOffsetY = 4;
        _stroke.apply(this, arguments);
        ctx.restore();
    }
};

let ctx = document.querySelector("#canvas").getContext('2d');
let myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: ["January", "February", "March", "April", "May", "June", "July"],
        datasets: [{
            data: [65, 59, 75, 64, 70, 30, 40],
            borderColor: '#07C',
            pointBackgroundColor: "#FFF",
            pointBorderColor: "#07C",
            pointHoverBackgroundColor: "#07C",
            pointHoverBorderColor: "#FFF",
            pointRadius: 4,
            pointHoverRadius: 4,
            fill: false,
            tension: 0.15
        }]
    },
    options: {
        responsive: false,
        tooltips: {
            displayColors: false,
            callbacks: {
                label: function(e, d) {
                    return `${e.xLabel} : ${e.yLabel}`
                },
                title: function() {
                    return;
                }
            }
        },
        legend: {
            display: false
        },
        scales: {
            yAxes: [{
                ticks: {
                    max: 90
                }
            }]
        }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<canvas id="canvas" width="400" height="210" style="background-color: #E4E8F0"></canvas>

【讨论】:

  • 我尝试了这个解决方案,它有效,但我的网格上也出现了蓝色阴影......我做错了什么?
【解决方案3】:

这适用于新版本的 Chart JS 我们可以创建一个插件对象并注册到图表JS,插件是开发者在创建图表时修改图表的一种方式,参考请看

https://riptutorial.com/chart-js/example/22332/plugins-introduction

为任何图表添加阴影的示例插件

var simpleplugin = {
beforeDraw : function(chartInstance)
{

    let _stroke = chartInstance.ctx.stroke;
    chartInstance.ctx.stroke = function () {

        chartInstance.ctx.save();
        chartInstance.ctx.shadowColor = 'gray';
        chartInstance.ctx.shadowBlur = 10;
        chartInstance.ctx.shadowOffsetX = 2;
        chartInstance.ctx.shadowOffsetY = 2;
        _stroke.apply(this, arguments)
        chartInstance.ctx.restore();
    }

    let _fill = chartInstance.ctx.fill;
    ctx.fill = function () {

        chartInstance.ctx.save();
        chartInstance.ctx.shadowColor = 'gray';
        chartInstance.ctx.shadowBlur = 10;
        chartInstance.ctx.shadowOffsetX = 2;
        chartInstance.ctx.shadowOffsetY = 2;
        _fill.apply(this, arguments)
        chartInstance.ctx.restore();
    }
}

}

$(function()
{
    Chart.pluginService.register(simpleplugin);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 2022-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多