Color chart numbers

1

I made a code in js and php, but I'm not finding where the color of the numbers that show the values above the points of the graph lines is set. I need to know at what time that red arrow in the numbers.

Code:

<div class="large-12 columns">
    <div class="header panel">
        <canvas id="canvasBG"></canvas>
    </div>
</div>

<script>

var dataBG = {
    labels: [ <?php foreach($class->Lista("BG") as $dados){ echo '"'.$dados->getQtTotalOrigem().'",'; }?> ],
    datasets: [{
        label: "Entregas",
        backgroundColor: window.chartColors.red,
        borderColor: window.chartColors.red,
        data: [ <?php foreach($class->Lista("BG") as $dados){ echo $dados->getQtTotalDestino().','; }?> ],
        fill: false,            
    }, {
        label: "Efetivas",
        backgroundColor: window.chartColors.blue,
        borderColor: window.chartColors.blue,
        data: [ <?php foreach($class->Lista("BG") as $dados){ echo $dados->getQtTotalFinal().','; }?> ],
        fill: false,
    }, {
        label: "Veiculos",
        fill: false,
        backgroundColor: window.chartColors.green,
        borderColor: window.chartColors.green,
        data: [ <?php foreach($class->Lista("BG") as $dados){ echo $dados->getQtAtrasadoOrigem().','; }?> ],
    }]

};

var ctx = document.getElementById("canvasBG").getContext("2d");
window.myBar = new Chart(ctx, {
    type: 'line',
    data: dataBG,
    options: {

        "hover": {
            "animationDuration": 0
        },
        "animation": {
            "duration": 1000,
            "onComplete": function() {
                var chartInstance = this.chart,
                ctx = chartInstance.ctx;

                ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, Chart.defaults.global.defaultFontStyle, Chart.defaults.global.defaultFontFamily);
                ctx.textAlign = 'center';
                ctx.textBaseline = 'bottom';


                this.data.datasets.forEach(function(dataset, i) {
                    var meta = chartInstance.controller.getDatasetMeta(i);
                    meta.data.forEach(function(bar, index) {
                        var data = dataset.data[index];
                        ctx.fillText(data, bar._model.x, bar._model.y - 1);
                    });
                });
            }
        },

        responsive: true,
        title:{
            display:true,
            text:'Apuração de entregas'
        },
        legend: {
            "display": true,
            position: 'bottom',
        },
        tooltips: {
            "enabled": false
        },
        scales: {
            xAxes: [{
                display: true,
                scaleLabel: {
                    display: false,
                    labelString: 'Month'
                }
            }],
            yAxes: [{
                display: true,
                scaleLabel: {
                    display: false,
                    labelString: 'Value'
                }
            }]
        }
    }
});
</script>

Chart:

    
asked by anonymous 19.10.2017 / 18:35

2 answers

2

The chartsjs has a number of attributes with specific functionality in the rendering object appearance.

An example for 'labels' of the caption (removed from the documentation itself):

var chart = new Chart(ctx, {
    type: 'bar',
    data: data,
    options: {
        legend: {
            display: true,
            labels: {
                fontColor: 'rgb(255, 99, 132)'
            }
        }
    }
});

In your case, you want to change the color of dataset , the function backgroundColor is a function of 'dataset' not 'label' , so it always has the same color, the last call.

In order to achieve the desired result, colors should be reported as a property of dataset , but in the form of array , one color for each label :

data: {
    labels: ["Entregas", "Efetivas", "Veiculos"],
    datasets: [{
        label: 'Dados',
        data: ['Dados'],
        backgroundColor: [
            '#D3E4F3'
        ],
        borderColor: [
            '#000',
            '#999',
            '#ccc'
        ],
        borderWidth: 1
    }]
}

The same thing goes for any dataset attribute, not just the data, but an array for every function you want out of data.

Functional example:

Chart.defaults.global.legend.labels.usePointStyle = true;

var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
    labels: ["Entregas", "Efetivas", "Veiculos"],
    datasets: [{
        label: 'Dados',
        data: [17,9,29],
        backgroundColor: [
            '#fff',
            '#000',
            '#ccc'
        ],
        borderColor: [
            '#000',
            '#999',
            '#ccc'
        ],
        fontColor: [
            '#000',
            '#999',
            '#ccc'
        ],
        borderWidth: 1
    }]
},
    options: {
        legend: {
            display: true,
            position: 'bottom',
            labels: {
                fontColor: '#333'
            }
        }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script><divclass="container">
  <canvas id="myChart"></canvas>
</div>

Sources:

Doc. About styles

Recommendation to read why not change colors if you do not know what you're doing:

8% Better - Data Graphics and Color Vision

  

UPDATE

To define the color of the values it is necessary to follow:

    datasets: [
            {
              label: '# of Votes',
              data: [12, 19, 3, 5, 2, 3],
            borderWidth: 1,
            backgroundColor: '#000',
            borderColor: '#000'
            },  
                {
                    label: '# of Points',
                    data: [7, 11, 5, 8, 3, 7],
                    borderWidth: 1,
            backgroundColor: '#ccc',
            borderColor: '#ccc'

                }
    ]

However, for charts in 'LINE' it will always put a single color on all lines, there is no documentation for type='line' no function indicating to change the color of 'value' , ie there is no way , please note that you are only setting the color of borda and background , never label of valor .

  

Why did all the colors look the same?

Because you set 'backgroudColor' , and the line with the highest values, will have its backgroundColor ahead of the others, notice that in your example, if the value of 'efetivas' becomes greater than 'entregas' the color of values will turn blue.

This may be a bug in the plugin, or even if it has been made to work like this, in short, you will not be able to change the color of values to a type=line without changing / extending the plugin.

PS: There may be some gambiarras for this, but I do not know some way that will work.

    
20.10.2017 / 13:14
0

You are in this section:

var dataBG = {
    labels: [ <?php foreach($class->Lista("BG") as $dados){ echo '"'.$dados->getQtTotalOrigem().'",'; }?> ],
    datasets: [{
        label: "Entregas",
        backgroundColor: window.chartColors.red, <-- Aqui
        borderColor: window.chartColors.red, <-- Aqui
        fontColor:  window.chartColors.red, <!-- Edit 1: Implementação do código. 
        data: [ <?php foreach($class->Lista("BG") as $dados){ echo $dados->getQtTotalDestino().','; }?> ],
        fill: false,            
    }, {
        label: "Efetivas",
        backgroundColor: window.chartColors.blue, <-- Aqui
        borderColor: window.chartColors.blue, <-- Aqui
        data: [ <?php foreach($class->Lista("BG") as $dados){ echo $dados->getQtTotalFinal().','; }?> ],
        fill: false,
    }, {
        label: "Veiculos",
        fill: false,
        backgroundColor: window.chartColors.green, <-- Aqui
        borderColor: window.chartColors.green, <-- Aqui
        data: [ <?php foreach($class->Lista("BG") as $dados){ echo $dados->getQtAtrasadoOrigem().','; }?> ],
    }]

};

If you want to use a different color, just use:

backgroundColor: 'rgb(255, 255, 255)',
borderColor: 'rgb(255, 255, 255)',
    
19.10.2017 / 18:41