how can i use chart.js to create a chart that has one time series line and one linear line on it at the same time?

Nemanja Vidačković

I want to draw a reference line in the time series chart. I tried to use two x axis and two y axis so I have like two graphs in one. the one would show time scale line and the other one would show linear line (reference line).

image example of what I want it to look like :

red and black horizontal line are the reference lines and the green one is the time series line

I tried in fiddle but its like the two X axis are connected and not scaling separately:

var data = {
  labels: [],
  datasets: [{
    fill: 'none',
    label: 'signal1',
    backgroundColor: 'rgba(75,192,192,0.4)',
    pointRadius: 5,
    pointHoverRadius: 7,
    borderColor: 'rgba(75,192,192,1)',
    borderWidth: 1,
    lineTension: 0,
    xAxisID: 'xAxis1',
    yAxisID: 'yAxis1',
    data: [{
        x: '2016-07-17',
        y: 44
      },
      {
        x: '2016-07-19',
        y: 50
      },
      {
        x: '2016-07-22',
        y: 84
      },
    ],
  }, {
    fill: 'none',
    label: 'signal2',
    lineTension: 0,
    xAxisID: 'xAxis2',
    yAxisID: 'yAxis2',
    data: [{
        x: 0,
        y: 55
      },
      {
        x: 1,
        y: 55
      },
    ]
  }],
};
var option = {
  scales: {
    yAxes: [{
      id: 'yAxis1',
      offset: true,
      gridLines: {
        color: 'rgba(151, 151, 151, 0.2)',
        display: true,
        zeroLineColor: '#979797',
        zeroLineWidth: 1,
        tickMarkLength: 15,
        drawBorder: true,
      },
      ticks: {
        beginAtZero: false,
        padding: 5,
        fontSize: 12,
        fontColor: '#222222',
      },
    }, {
      id: 'yAxis2',
      gridLines: {
        color: 'rgba(151, 151, 151, 0.2)',
        display: false,
        drawBorder: false,
      },
      ticks: {
        beginAtZero: false,
      },
    }],
    xAxes: [{
      id: 'xAxis1',
      offset: true,
      bounds: 'data',
      type: 'time',
      distribution: 'linear',
      time: {
        unit: 'day',
        displayFormats: {
          day: 'D.M.YYYY',
        },
      },
      gridLines: {
        display: true,
        color: 'rgba(151, 151, 151, 0.2)',
        zeroLineColor: '#979797',
        zeroLineWidth: 1,
        tickMarkLength: 5,
        drawBorder: true,
      },
      ticks: {
        source: 'auto',
        autoSkip: true,
        autoSkipPadding: 30,
        maxRotation: 0,
        padding: 2,
        fontSize: 12,
        fontColor: '#222222',
      },
    }, {
      id: 'xAxis2',
      type:"linear",
      gridLines: {
        display: false,
        drawBorder: false,
      },
    }]
  }
};

https://jsfiddle.net/2gyk9v5e/15/

uminder

This can be done with the Plugin Core API. The API offers different hooks that may be used for executing custom code. In your case, you can use the afterDraw hook to draw the desired lines directly on the canvas using CanvasRenderingContext2D.

plugins: [{
  afterDraw: chart => {
    var ctx = chart.chart.ctx;
    var xAxis = chart.scales['x-axis-0'];
    var yAxis = chart.scales['y-axis-0'];
    ctx.save();
    chart.data.datasets[0].refLines.forEach(r => {
      var y = yAxis.getPixelForValue(r.y);
      ctx.strokeStyle = r.color;
      ctx.beginPath();
      ctx.moveTo(xAxis.left, y);
      ctx.lineTo(xAxis.right, y);
      ctx.stroke();
    });
    ctx.restore();
  }
}],

Above code assumes that the reference lines are defined inside your dataset through the following definition.

refLines: [
  { y: 45, color: '#0be059' },
  { y: 49, color: '#fc3503' }
]

Please take a look at your amended code and see how it works.

new Chart('canvas', {
  type: 'line',
  plugins: [{
    afterDraw: chart => {
      var ctx = chart.chart.ctx;
      var xAxis = chart.scales['x-axis-0'];
      var yAxis = chart.scales['y-axis-0'];
      ctx.save();
      chart.data.datasets[0].refLines.forEach(r => { 
        var y = yAxis.getPixelForValue(r.y);
        ctx.strokeStyle = r.color;
        ctx.beginPath();
        ctx.moveTo(xAxis.left, y);
        ctx.lineTo(xAxis.right, y);
        ctx.stroke();
      });
      ctx.restore();
    }
  }],
  data: {
    datasets: [{
      fill: false,
      label: 'signal1',
      backgroundColor: 'rgba(75,192,192,0.4)',
      pointRadius: 5,
      pointHoverRadius: 7,
      borderColor: 'rgba(75,192,192,1)',
      borderWidth: 1,
      lineTension: 0,
      data: [
        { x: '2016-07-14', y: 44 },
        { x: '2016-07-15', y: 52 },
        { x: '2016-07-16', y: 45 },
        { x: '2016-07-17', y: 47 },
        { x: '2016-07-18', y: 35 },
        { x: '2016-07-19', y: 46 },
        { x: '2016-07-20', y: 50 },
        { x: '2016-07-21', y: 44 }
      ],
      refLines: [
        { y: 45, color: '#0be059' },
        { y: 49, color: '#fc3503' }
      ]
    }]
  },
  options: {
    scales: {
      yAxes: [{
        gridLines: {
          color: 'rgba(151, 151, 151, 0.2)',
          display: false,
          drawBorder: false
        }
      }],
      xAxes: [{
        offset: true,
        bounds: 'data',
        type: 'time',
        time: {
          unit: 'day',
          displayFormats: {
            day: 'D.M.YYYY',
          }
        },
        gridLines: {
          color: 'rgba(151, 151, 151, 0.2)',
          zeroLineColor: '#979797',
          zeroLineWidth: 1,
          tickMarkLength: 5,
          drawBorder: true
        }
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.bundle.min.js"></script>
<canvas id="canvas" height="80"></canvas>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

dc.js time series line chart

Vega Multi Series Line Chart - Use Dashed Line for One Series

Time series line chart is not displayed

R Time series line chart

How to make a Line Chart with multiple Series where one series has null values

How to create bar and line chart in one chart using ggplot

How to create a multi line chart with with dynamic x and y axis in one graph using chart js?

How can I delete a line between time gap in Line chart (Bokeh)?

How can I parse many files at the same time with one line commands(Linux/Unix)

How can I read one line at a time from a trio ReceiveStream?

How can I get the contents of a file one line at a time?

How can I pass variables to a script one line at a time in bash?

SSRS - How do I format SQL data to generate line chart of time series?

How can I create a horizontal scrolling Chart.js line chart with a locked y axis?

Can i create a Circle Hole over One that Already Exist in MpAndroid Line Chart?

How do I prevent Power BI line chart drawing a line through time series points when there is no value for some intervals?

How to create multi-series line chart

How to use time series in c3.js chart?

How can I assign green for an uptrend in line chart in chart-js and red for a downtrend in chart-js?

Line chart doesn't work with type time chart.js

Drawing Real Time Timeseries Line Chart with Lightning Chart JS?

How to create a line chart with two line, with one of them being filled, and the other staying on the foreground of the filled line?

How can I combine a line and scatter on same plotly chart?

Combine scatter, boxplot and linear regression line on one chart ggplot R

chart.js - how to draw and manage line when only one label present in chart js Linechart

getting unusual line chart on weekly time series data in matplotlib

Any way to correctly make weekly time series line chart in matplotlib?

Google Line chart with dual Y axis but with 3 or more time series

Flot line chart time xaxis series fixed labels