Chart.js how to use scriptable options
Asked Answered
S

2

2

I am migrating chart.js to 3.x as per the migration guide.
https://www.chartjs.org/docs/master/getting-started/v3-migration/

I am trying to set the xAxis zeroLineColor to "#FFFFFF" and I want to have the Grid line color as "#00FFFF" but as per the chart.js migration document document scales.[x/y]Axes.zeroLine* options of axes were removed. Use scriptable scale options instead.

I am not sure how to use scriptable scale options for setting the xAxis line zero to white.

Update:
Working example:
https://jsfiddle.net/g4vq7o2b/2/

Sum answered 20/9, 2020 at 8:31 Comment(0)
B
4

In order to obtain different colors for the grid's zero line, you could define an array of colors for the option gridLines.color.

gridLines: {
  drawBorder: false,
  color: ["#FFFFFF", "#00FFFF", "#00FFFF", "#00FFFF", "#00FFFF"]
}

Since gridLines.color is a scriptable options, it also accepts a function which is invoked with a context argument for each of the underlying data values as follows:

gridLines: {
  drawBorder: false,
  color: context => context.tick.value == 0 ? "#FFFFFF" : "#00FFFF"
}

Please take a look at below runnable code and see how it works.

new Chart(document.getElementById("myChart"), {
  type: "line",
  data: {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
      label: "My Dataset",
      data: [2, 3, 1, 4, 2, 4, 2],
      fill: false,
      backgroundColor: "rgba(0, 255, 0, 0.3)",
      borderColor: "rgb(0, 255, 0)"
    }]
  },
  options: {
    scales: {
      y: {
        min: 0,
        ticks: {
          stepSize: 1
        },
        gridLines: {
          drawBorder: false,
          color: context => context.tick.value == 0 ? "#FFFFFF" : "#00FFFF"
        }
      },
      x: {
        gridLines: {
          drawBorder: false,
          color: context => context.tick.value == 0 ? "#FFFFFF" : "#00FFFF"
        }
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.0-beta/chart.min.js"></script>
<canvas id="myChart" height="90"></canvas>
Baggs answered 20/9, 2020 at 13:57 Comment(4)
Thanks, I have the scatter chart and I am not able to get it working as described. Also do we need to set the yAxis tick 0 to the '#FFFFFF' color to have the xAxis zero line color set to '#FFFFFF'?Sum
@User7723337: Can you post a runnable version of your code?Baggs
Updated question with a working example. I need to display grid lines only for xAxis with a different color for grid lines and zero line. Also yAxis tick line color is not the same as yAxis zero line color. Please refer to the working example. jsfiddle.net/g4vq7o2b/2Sum
@User7723337: Apparently the color of the grid's zero line is also controlled through the gridLines.color (see chartjs.org/samples/master/scales/gridlines-scriptable.html). I think I answered your original question that was "how to use scriptable options?".Baggs
E
0

Using chart_v3.7.0.js cartesian time axis, for me, configuring zeroline and other gridlines works, using scriptable options. Apply property for "context.tick.value == 0" (=baseline) like: ...

options: { 
            scales: {

            
                x: {    
                        gridLines: {
                          color: "#00ff00",
                        },
                        ticks: {        font: { size: 30    },
                                        maxRotation: 90,
                                        minRotation: 90,
                                },
                        type: 'time',       
                        time: {
                                /*tooltipFormat: "DD.MM hh:mm",*/
                                displayFormats: {
                                    minute: 'HH:mm',
                                    hour: 'HH:mm',
                                    day: 'dd.MMM',
                                }
                        },
                },
                            
                y: {    
                        grid: {
                            color: (line) => (line.index === 0 ? 'red' : 'rgba(0, 0, 0, 0.1)') //color of lowest horizontal grid line
                            color: context => context.tick.value == 0 ? "#FFFFFF" : "#00FFFF", // zero-line baseline color
                            lineWidth: context => context.tick.value == 0 ? 3 : 1, // zero-line baseline width
                        },
                        position: 'right', // show axis on the right
                        title: {        display: true,
                                        rotation: 0,
                                        text: "°C",                 }, 
                    },
                                        
            },
Etch answered 11/2, 2022 at 22:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.