Display Time In Y Axis - Bubble Chart

只愿长相守 提交于 2019-12-12 19:45:24

问题


I came across Bubble Chart (latest addition) in ng2-charts. I am trying to show data according to the time in Y-axis and values in X-axis. My Data is like x:[10,35,60] and y:["7.00 AM"] and r will the same value of x. Basically I want to show multiple data for one date but the sample dataset given is different from my dataset. Can you help me out? and one more thing, I want to hide r from tooltip.

Sample code

HTML

 <div style="display: block">
      <canvas baseChart [datasets]="bubbleChartData" [options]="bubbleChartOptions"
    [colors]="bubbleChartColors" [legend]="bubbleChartLegend" [chartType]="bubbleChartType"
    (chartHover)="chartHovered($event)" (chartClick)="chartClicked($event)"></canvas>
    </div>

TS

 public bubbleChartType: ChartType = 'bubble';
  public bubbleChartLegend = true;

  public bubbleChartData: ChartDataSets[] = [
    {
      data: [
        { x: 10, y: 10, r: 10 },
        { x: 15, y: 5, r: 15 },
        { x: 26, y: 12, r: 23 },
        { x: 7, y: 8, r: 8 },
      ],
      label: 'Series A',
      backgroundColor: 'green',
      borderColor: 'blue',
      hoverBackgroundColor: 'purple',
      hoverBorderColor: 'red',
    },
  ];

This Y axis supports only numeric numbers but I want to bind time like 11:15 AM

What I Want


回答1:


You can use a custom label formatter to display time values instead of numbers.

times = [
  "3:00 am",
  "7:00 am",
  "11:00 am",
  "3:00 pm",
  "7:00 pm",
  "11:00 pm",
]

public bubbleChartOptions: ChartOptions = {
  responsive: true,
  scales: {
    xAxes: [{ ticks: { min: 0, max: 30, } }],
    yAxes: [{
      ticks: {
        min: 0, max: 5,
        callback: value => this.times[value]
      }
    }]
  }
};

public bubbleChartData: ChartDataSets[] = [
  {
    data: [
      { x: 7, y: 0, r: 8 },
      { x: 10, y: 1, r: 10 },
      { x: 15, y: 2, r: 15 },
      { x: 26, y: 3, r: 23 },
    ],
    label: 'Series A',
  },
];

See this stackblitz for example.

Here's the image output:



来源:https://stackoverflow.com/questions/55058468/display-time-in-y-axis-bubble-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!