如何通过单击图表栏来更改颜色?

时间:2018-09-27 20:01:16

标签: javascript ecmascript-6 chart.js

我可以使用onclick函数进行颜色更改,但是我应该遵循以下逻辑:

1)最后一个栏的颜色总是与其他栏不同。要启动图表,就像单击第一个栏一样。

2)单击其他栏时,颜色必须进行更改,以向用户显示该栏已被单击,但是先前的栏应返回其正常颜色..

我在使用此逻辑时遇到困难,并且正在使用chartjs 2.x

class ChartVenda {
    constructor(_id, _dataJSON){
        this.id           = _id;
        this.dataJSON     = _dataJSON;
        this.chartElement = document.getElementById(_id).getContext('2d');
    }

    set chartObject(_object){ this.object = _object; }
    get chartObject(       ){ return this.object;    }

    get labels(){
        return ['jan', 'fev', 'mar', 'abr', 'mai', 'jun', 'jul', 'ago', 'set', 'out', 'nov', 'dez'];
    }

    get bgColors(){
        return [
            'rgb(202, 216, 229)',
            'rgb(202, 216, 229)',
            'rgb(202, 216, 229)',
            'rgb(202, 216, 229)',
            'rgb(202, 216, 229)',
            'rgb(202, 216, 229)',
            'rgb(202, 216, 229)',
            'rgb(202, 216, 229)',
            'rgb(202, 216, 229)',
            'rgb(202, 216, 229)',
            'rgb(202, 216, 229)',
            'rgb(202, 216, 229)'
        ]
    }

    get borderColors(){
        return [
            'rgba(0,0,0,0.3)',
            'rgba(0,0,0,0.3)',
            'rgba(0,0,0,0.3)',
            'rgba(0,0,0,0.3)',
            'rgba(0,0,0,0.3)',
            'rgba(0,0,0,0.3)',
            'rgba(0,0,0,0.3)',
            'rgba(0,0,0,0.3)',
            'rgba(0,0,0,0.3)',
            'rgba(0,0,0,0.3)',
            'rgba(0,0,0,0.3)',
            'rgba(0,0,0,0.3)',
        ]
    }

    createChart(){
        this.generateChart();
        this.addChartOptions();
        this.addDataJsonInChart();
        this.addChartLabels();
        this.addChartDatasets();
        this.addAnimation();

        this.chartObject.update();
        console.log(this.chartObject.data.datasets)
    }

    generateChart(){
        let chartVenda = new Chart(this.chartElement, { type: 'bar'});
        this.chartObject = chartVenda;
    }

    addDataJsonInChart(){
        this.chartObject.data.dataJSON = this.dataJSON;
    }

    addChartLabels(){
        let counterLabels = 0;
        this.dataJSON.records.forEach( () => {
            counterLabels += 1;
            this.chartObject.data.labels.push(this.labels[counterLabels-1]);
        });
    }

    addChartDatasets(){
        let arrDeltaVendam2 = [];
        this.dataJSON.records.forEach( (_item) => {
            arrDeltaVendam2.push(_item['Δ% Venda m²']);
        });

        this.chartObject.data.datasets.push({
            label:           `ae`,
            data:            arrDeltaVendam2,
            backgroundColor: 'rgb(202, 216, 229)',
            borderColor:     'rgb(202, 216, 229)',
        });

        this.chartObject.update();
    }

    addChartOptions(){
        this.chartObject.options = {
            responsive:          true,
            maintainAspectRatio: false,
            legend: { display:   false },
            scales: {
                yAxes: [{ ticks: {display: false, fontColor: 'white'},  gridLines: {display: false, drawBorder: false} }],
                xAxes: [{ ticks: {fontSize: 15,   fontColor: 'white' }, gridLines: {display: false, drawBorder: false} }],
            },
            onClick: function(_event, _value) {
                let datasetIndex,
                    dataset;

                if (_value.length) {
                    this.chart.data.dataJSON.records.forEach( (item) => {
                        if(item['Competência'] === _value[0]._model.label){
                            const VENDA_METROS_TESTE = document.getElementById('primary-jumbotron-subtitle-indicator-m²'),
                                  VENDA_ABS_TESTE    = document.getElementById('primary-jumbotron-subtitle-indicator-abs');

                            VENDA_METROS_TESTE.innerHTML = `${item['Venda m²']} m²`;
                            VENDA_ABS_TESTE.innerHTML    = `${item['Venda']} abs`;
                        }
                      });

                      datasetIndex = _value[0]._datasetIndex;

                      // Reset old state
                      dataset = this.chart.data.datasets[datasetIndex];
                      dataset.backgroundColor = dataset.backgroundColor.slice();
                      dataset.backgroundColor = 'rgb(133, 222, 252)'; // click color
                  } else {
                    // remove hover styles
                    for (datasetIndex = 0; datasetIndex < myChart.data.datasets.length; ++datasetIndex) {
                      dataset = this.chart.data.datasets[index];
                      dataset.backgroundColor = dataset.backgroundColor.slice();
                    }
                }
            }
        }
    }

    addAnimation(){
        this.chartObject.options.animation = {
            duration: 1000,
            onProgress: function() {
                /** @description <CRIA O EFEITO DE TEXTO EM CIMA DAS BARRAS> **/
                const CHART_OBJECT = this.chart;
                CHART_OBJECT.ctx.font         = Chart.helpers.fontString('17', 'normal', Chart.defaults.global.defaultFontFamily);
                CHART_OBJECT.ctx.fillStyle    = 'white';
                CHART_OBJECT.ctx.textAlign    = 'center';
                CHART_OBJECT.ctx.textBaseline = 'bottom';
                Chart.helpers.each(this.data.datasets.forEach(function (_dataset, _index1) {
                    Chart.helpers.each(CHART_OBJECT.getDatasetMeta(_index1).data.forEach(function (_bar, _index2) {
                        let centerPoint = _bar.getCenterPoint();
                        CHART_OBJECT.ctx.fillText(_dataset.data[_index2], centerPoint.x, centerPoint.y);
                    }), this);
                }), this);
            },
        }
    }


}

1 个答案:

答案 0 :(得分:1)

我不明白你的第一点,但是要解决你的第二点:

以下代码段为每个栏设置了默认颜色(“浅灰色”)。单击条形时,所有条形均设置为原始颜色,并且所单击的条形设置为“红色”。这种方法的好处是每个条形的起始颜色可以不同,并且可以正确地切换为“红色”和“红色”。

let background = ['lightgrey', 'lightgrey', 'lightgrey', 'lightgrey'],
  myChart = new Chart(document.getElementById('chart'), {
    type: 'bar',
    data: {
      labels: ['a', 'b', 'c', 'd'],
      datasets: [{
        label: 'series1',
        data: [7, 10, 8, 2],
        backgroundColor: background.slice() // using .slice here 'clones' the array.
      }]
    },
    options: {
      maintainAspectRatio: false,
      onClick: function(e, elems) {
        if (elems.length) {
          elems[0]._chart.config.data.datasets[0].data.forEach((value, index) => {

            // set element to the original colour (resets all).
            elems[0]._chart.config.data.datasets[0].backgroundColor[index] = background[index];
            if (index == elems[0]._index) {
              // set the clicked element to red.
              elems[0]._chart.config.data.datasets[0].backgroundColor[index] = 'red';
            }
          });
          myChart.update(); // if you don't want the animation use 'myChart.update(0);' instead.
        }
      }
    }
  });
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
<canvas id="chart"></canvas>

注释:

  • 图例使用第一个点的颜色,因此单击时图例的颜色也会更改。
  • 这仅适用于第一个数据集(elems[0]...datasets[0])。