How can I hide data gaps in plotly?

前端 未结 1 1050
难免孤独
难免孤独 2021-01-12 15:40

I have datasets that may include large gaps in data, and I want to chart the data without plotly automatically filling in the gaps with blank space.

Example

相关标签:
1条回答
  • 2021-01-12 16:33

    Use this in your config.

    connectgaps: true
    

    I actually tried this out by viewing the python version of this library.

    Plotly :: Python Docs :: Line Charts :: Connect Data Gaps

    var rawData = [
      {date: new Date(2017, 01, 10), value: 5},
      {date: new Date(2017, 01, 11), value: 6},
      {date: new Date(2017, 01, 12), value: 8},
      {date: new Date(2017, 01, 13), value: 13},
      {date: new Date(2017, 01, 14), value: null}, //Null to avoid plotting the line over the gap
      {date: new Date(2017, 01, 20), value: 12},
      {date: new Date(2017, 01, 21), value: 14},
      {date: new Date(2017, 01, 22), value: 8},
      {date: new Date(2017, 01, 23), value: 9},
      {date: new Date(2017, 01, 24), value: 11},
      {date: new Date(2017, 01, 25), value: 8},
      {date: new Date(2017, 01, 26), value: 6},
      {date: new Date(2017, 01, 27), value: 7}
    ];
    
    let trace1 = {
      name: 'values',
      type: 'scatter',
      mode: 'lines+markers',
      x: getData(rawData, 'date'),
      y: getData(rawData, 'value'),
      connectgaps: true             // <-- HERE
    }
    
    Plotly.newPlot('myChart', [trace1]);
    
    function getData(input, propName) {
      let output = [];
    
      for (let i = 0; i < input.length; i++) {
        output.push(input[i][propName]);
      }
      return output;
    }
    <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
    <div id="myChart"></div>


    Edit

    I guess the best way to approach this it to treat the x-axis like a category axis.

    var rawData = [
      { date: '2017-02-10', value:  5 },
      { date: '2017-02-11', value:  6 },
      { date: '2017-02-12', value:  8 },
      { date: '2017-02-13', value: 13 },
      { date: '2017-02-20', value: 12 },
      { date: '2017-02-21', value: 14 },
      { date: '2017-02-22', value:  8 },
      { date: '2017-02-23', value:  9 },
      { date: '2017-02-24', value: 11 },
      { date: '2017-02-25', value:  8 },
      { date: '2017-02-26', value:  6 },
      { date: '2017-02-27', value:  7 }
    ];
    
    let trace1 = {
      name: 'values',
      type: 'scatter',
      mode: 'lines+markers',
      x: getData(rawData, 'date').map((d, i) => i),
      y: getData(rawData, 'value'),
    }
    
    let layout = {
      xaxis: {
        title: 'Date',
        tickvals: getData(rawData, 'date').map((d, i) => i).filter(filterEven),
        ticktext: getData(rawData, 'date').map(d => moment(d).format('MMM DD')).filter(filterEven)
      }
    }
    
    Plotly.newPlot('myChart', [trace1], layout);
    
    function filterEven(v, i) { return i % 2 === 0; }
    function getData(input, prop) { return input.map(v => v[prop]); }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
    <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
    <div id="myChart"></div>

    0 讨论(0)
提交回复
热议问题