亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在懸停條形圖上添加“陰影”?

如何在懸停條形圖上添加“陰影”?

偶然的你 2022-07-08 09:51:28
在chartJS中懸停索引時是否可以添加一些陰影?Somethink like in this answer在點懸停時添加一條線,但我會將它與條形圖一起使用并使其看起來像這樣:這是我的實際圖表jsfiddle <div class="chart-area chart-reparti">      <canvas id="chartReparti" width="1600" height="250"></canvas> </div>        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script>var optionsReparti = {        maintainAspectRatio: false,        legend: {            display: true        },        tooltips: {            backgroundColor: '#f5f5f5',            titleFontColor: '#333',            bodyFontColor: '#666',            displayColors: true,            mode: 'index',            intersect: 0        },        responsive: true,        scales: {            yAxes: [{                id: 'importo',                gridLines: {                    drawBorder: false,                    borderDash: [4, 8],                    color: 'rgba(0,132,255,0.4)',                },                ticks: {                    beginAtZero: true,                    userCallback: function (value, index, values) {                        return "€" + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");                    }                }            }, {                id: 'qta',                gridLines: {                    drawBorder: false,                    borderDash: [4, 8],                    color: 'rgba(247,136,0,0.4)',                },                ticks: {                    beginAtZero: true                }            },            ],            xAxes: [{                categoryPercentage: 1,                barPercentage: 0.4,                gridLines: {                    drawBorder: false,                    color: 'rgba(225,78,202,0.1)',                    zeroLineColor: "transparent",                }            }]        }    };實際上,從這個答案中得到的修改插件的代碼不包括在內,因為它根本不起作用。
查看完整描述

1 回答

?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

你可以在繪制之前自定義代碼


工作演示:https ://jsfiddle.net/4rasm1hc/


let draw = Chart.controllers.line.prototype.draw;

Chart.controllers.line = Chart.controllers.bar.extend({

    draw: function() {

        draw.apply(this, arguments);

        let ctx = this.chart.chart.ctx;

        let _stroke = ctx.stroke;

        ctx.stroke = function() {

            ctx.save();

            ctx.shadowColor = '#000000';

            ctx.shadowBlur = 10;

            ctx.shadowOffsetX = 0;

            ctx.shadowOffsetY = 4;

            _stroke.apply(this, arguments)

            ctx.restore();

        }

    }

});


Chart.defaults.LineWithLine = Chart.defaults.bar;

Chart.controllers.LineWithLine = Chart.controllers.bar.extend({

   draw: function(ease) {

      Chart.controllers.line.prototype.draw.call(this, ease);


      if (this.chart.tooltip._active && this.chart.tooltip._active.length) {

         var activePoint = this.chart.tooltip._active[0],

             ctx = this.chart.ctx,

             x = activePoint.tooltipPosition().x+15,

             topY =6000,

             width=activePoint._view.width,

             bottomY = 0;

        console.log(activePoint);

         // draw line

         ctx.save();

         ctx.beginPath();

         ctx.moveTo(x, topY);

         ctx.lineTo(x+width-10, bottomY+30);

         ctx.lineWidth = width*4;

         ctx.strokeStyle = '#e5e0e01a';

         ctx.stroke();

         ctx.restore();

      }

   }

});


查看完整回答
反對 回復 2022-07-08
  • 1 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號