1

I work with Angular v13 and chartjs v3, I also install chartjs-plugin-zoom, it works perfect for bar chart, but for the time bar chart, it does not work as expected, here is my problem:

When I get data from api, example 50 records in 24hours and I show it in chart, I only want to show 10 bars (user can pan left or right to see next data) but the chart always show all bar

24 hours time bar chart

I want to make it show like this one, this I get data by date, 10 date show alwasy 10 or less bars

10 days time bar chart

So how to archive the limit number of bar chart to show? Like I getting more than 10 records, how to make it only show 10, then user can pan left to see next data

There are some option I use in my chart

{
  animation: {
    duration: 0,
  },
  maintainAspectRatio: false,
  responsive: true,
  scales: {
    y: {
      beginAtZero: true,
      grid: {
        drawBorder: false,
      },
      grace: '10%',
    },
    x: {
      grid: {
        display: false,
      },
      type: 'time',
      display: true,
      time: {
        unit: 'day',
        unitStepSize: 1,
        displayFormats: {
          day: ChartOptionConstants.DATE_FORMAT,
        },
      },
      title: {
        display: true,
        text: 'DOC',
      },
      ticks: {
        autoSkip: true,
        maxRotation: 0,
        maxTicksLimit: 8
      },
    },
  },
  interaction: {
    mode: 'index',
    intersect: false,
  },
  plugins: {
    title: {
      display: false,
      text: this.chartTitle,
    },
    legend: {
      display: true,
      labels: {
        font: {
          size: 9,
        },
        usePointStyle: true,
      },
    },
    tooltip: {
      position: 'top',
      caretSize: 0,
    },
  },
};

Zoom plugin options

{
  pan: {
    enabled: true,
    mode: 'x',
  },
  zoom: {
    wheel: {
      enabled: true,
    },
    pinch: {
      enabled: true,
    },
    mode: 'x',
  },
};

2 Answers2

1

The zoom plugin is providing the limits node in its configuration where you can set the limtis on axes.

Maybe it could help your use case: https://www.chartjs.org/chartjs-plugin-zoom/latest/guide/options.html#limits

user2057925
  • 2,377
  • 1
  • 12
  • 12
  • 1
    Thanks man, after some research I apply the limit for zoom/pan and using zoomScale from this post: https://stackoverflow.com/questions/72015770/chartjs-how-to-pan-programmatically-by-time-values-instead-of-pixels?rq=1 I can finally fix my chart – Khánh Trần Hoàng Aug 10 '22 at 08:17
0

This is what worked for me. You can hardcode it to zoom limits in the chart configs like below.

const config = {
    type: 'line',
    data: data,
    options: {
        animation: false,
        spanGaps: true,
        showLine: true,
        animation: {
            duration: 0 // general animation time
        },
        hover: {
            animationDuration: 0 // duration of animations when hovering an item
        },
        responsiveAnimationDuration: 0, // animation duration after a resize
        autoSkipPadding: 100,
        plugins: {
            zoom: {
                limits: {
                    x: {
                          minRange: getMinZoomRange(), // This is smallest time window you want to zoom into
                          min: 1680516080000,
                          max: 1680616080000,
                    },
                },
                pan: {
                    enabled: true,
                    mode: 'x'
                },
                zoom: {
                    wheel: {
                        enabled: true,
                    },
                    pinch: {
                        enabled: true
                    },
                    mode: 'x',
                }
            }
        },
        scales: {
            x: {
                type: 'time',
                time: {
                    // tooltipFormat: "yyyy-MMM-dd HH:mm",
                    displayFormats: {
                        day: "d-MMM HH:mm",
                        millisecond: "HH:mm:ss.SSS",
                        second: "HH:mm:ss",
                        minute: "HH:mm",
                        hour: "d-MMM HH:mm",
                    },
                    // unit: granularity.toLowerCase(),
                },
                // min: 0,
                // max: 0,
                ticks: {
                    source: 'auto'
                },
                displayFormats: {
                    second: 'MMM YYYY'
                }
            },
            y: {
                beginAtZero: true,
                title: {
                    display: true,
                    text: 'Time in Seconds'
                }
            },
            requests: {
                type: 'linear',
                position: 'right',
                min: 0,
                max: 100
            }
        }
    }
};

Then you can programmatically change it like below. dataChart is the Chart object.

dataChart.config.options.plugins.zoom.limits.x.min = appDataSet['timeStamps'][0];
dataChart.config.options.plugins.zoom.limits.x.max = appDataSet.timeStamps[appDataSet['timeStamps'].length - 1];
ycr
  • 12,828
  • 2
  • 25
  • 45