Search code examples
javascriptchartsapexcharts

apexcharts different options for axis range


Let's take a look at the simple chart data:

it has (x,y) pairs:

x y
0 -3
1 2
2 7
3 8
4 15
5 0

The idea is to create a basic line chart, using VueJS in my case, but the idea can be generalized to JavaScript.

I have a series array of objects, where each object has x and y coordinates:

series = [
  {
    x: 0,
    y: -3
  },
  {
    x: 1,
    y: 2
  },
  ...
]

This series is part of options object:

const options = { 
  chart: {
    type: 'line'
  },
  series: series 
}

const chart = new ApexCharts(document.querySelector("#chart"), options);

chart.render();

And the chart is rendered. Now, let's say I want to append the data to that chart - add 2 new (x,y) pairs

const newData = [
  {
    x: 6,
    y: 20
  },
  {
    x: 7,
    y: -10
  }
]

chart.appendData([{ data: chartData }])

I would also like that newly appendedData has, for example, different color, fill, or something else - so newly added data displays differently than old data.

Feel free to point me to documentation if I missed anything, but I searched through apex chart methods, and the only thing that looks remotely close to this would be inside

updateOptions() method, the redrawPath flag (updateOptions docs:

When the chart is re-rendered, should it draw from the existing paths or completely redraw the chart paths from the beginning. By default, the chart is re-rendered from the existing paths


Solution

  • In order to style the new data differently, you'll want to put these data points into a different series using the appendSeries method:

    const newData = [
      {
        x: 6,
        y: 20
      },
      {
        x: 7,
        y: -10
      }
    ]
    
    chart.appendSeries({
      name: "series-2", // optional
      data: newData
    })
    

    Most of the styling in ApexCharts is done based on series (more specifically seriesIndex). So by placing the new data in a separate series you'll be able to style this second series using an array of, for example, colors.

    You could either specify the color you would like to use as you append the new series of data using the updateOptions method you mention, or you can specify it in advance.

    chartOptions: {
      colors: ["#546E7A", "#E91E63"],
    }
    

    When working with "Numeric paired values in XY properties", the xaxis type also has to be explicitly set to numeric:

    chartOptions: {
      xaxis: {
        type: 'numeric',
      },
    }
    

    The tricky bit comes when you want to add more data a second time (or third, more time). There are two approaches I can think of here:

    1. Shuffle the existing data across to the original series (append series-2 to series-1) - and overwrite series-2 with your new data. You don't need to edit the colors in chartOptions.
    2. You could shuffle the colors along. If you want all "old" data to have the same color, simply prepend the colors array with your base color every time you add a new series. Or if you want each series to have a different color, just append a color every time you add a new series.