“34139ede051ea8a10643c615076cb9dadc2a0942”上不存在“drivers/git@gitcode.net:openeuler/kernel.git”
chart.js 3.0 KB
Newer Older
1 2
'use strict';

N
nem035 已提交
3 4
const Tracer = require('./tracer');

J
Jason Park 已提交
5 6 7 8 9 10 11 12
class ChartTracer extends Tracer {
  static getClassName() {
    return 'ChartTracer';
  }

  constructor(name) {
    super(name);

K
Kevin Nadro 已提交
13 14 15
    this.selectColor = '#2962ff';
    this.notifyColor = '#c51162';
    this.defaultColor = 'rgb(136, 136, 136)';
J
Jason Park 已提交
16

J
Jason Park 已提交
17
    if (this.isNew) initView(this);
J
Jason Park 已提交
18
  }
T
TornjV 已提交
19

J
Jason Park 已提交
20
  setData(C) {
J
Jason Park 已提交
21 22 23 24 25
    if (super.setData.apply(this, arguments)) {
      this.chart.config.data.datasets[0].data = C;
      this.chart.update();
      return true;
    }
J
Jason Park 已提交
26

J
Jason Park 已提交
27
    var color = [];
K
Kevin Nadro 已提交
28
    for (var i = 0; i < C.length; i++) color.push(this.defaultColor);
J
Jason Park 已提交
29 30 31 32 33 34
    this.chart.config.data = {
      labels: C.map(String),
      datasets: [{
        backgroundColor: color,
        data: C
      }]
J
Jason Park 已提交
35
    };
J
Jason Park 已提交
36
    this.chart.update();
J
Jason Park 已提交
37 38 39
  }

  _notify(s, v) {
J
Jason Park 已提交
40 41 42 43 44 45
    this.manager.pushStep(this.capsule, {
      type: 'notify',
      s: s,
      v: v
    });
    return this;
J
Jason Park 已提交
46 47 48
  }

  _denotify(s) {
J
Jason Park 已提交
49 50 51 52 53
    this.manager.pushStep(this.capsule, {
      type: 'denotify',
      s: s
    });
    return this;
J
Jason Park 已提交
54 55 56
  }

  _select(s, e) {
J
Jason Park 已提交
57 58 59 60 61 62
    this.manager.pushStep(this.capsule, {
      type: 'select',
      s: s,
      e: e
    });
    return this;
J
Jason Park 已提交
63 64 65
  }

  _deselect(s, e) {
J
Jason Park 已提交
66 67 68 69 70 71
    this.manager.pushStep(this.capsule, {
      type: 'deselect',
      s: s,
      e: e
    });
    return this;
J
Jason Park 已提交
72 73 74
  }

  processStep(step, options) {
J
Jason Park 已提交
75 76
    switch (step.type) {
      case 'notify':
J
Jason Park 已提交
77
        if (step.v !== undefined) {
J
Jason Park 已提交
78 79
          this.chart.config.data.datasets[0].data[step.s] = step.v;
          this.chart.config.data.labels[step.s] = step.v.toString();
T
TornjV 已提交
80
        }
J
Jason Park 已提交
81 82
      case 'denotify':
      case 'select':
J
Jason Park 已提交
83
      case 'deselect':
K
Kevin Nadro 已提交
84
        let color = step.type == 'notify' ? this.notifyColor : step.type == 'select' ? this.selectColor : this.defaultColor;
J
Jason Park 已提交
85 86 87 88 89 90 91 92
        if (step.e !== undefined)
          for (var i = step.s; i <= step.e; i++)
            this.chart.config.data.datasets[0].backgroundColor[i] = color;
        else
          this.chart.config.data.datasets[0].backgroundColor[step.s] = color;
        this.chart.update();
        break;
      default:
J
Jason Park 已提交
93
        super.processStep(step, options);
J
Jason Park 已提交
94
    }
J
minor  
Jason Park 已提交
95
  }
J
Jason Park 已提交
96 97 98 99 100 101

  resize() {
    super.resize();

    this.chart.resize();
  }
J
Jason Park 已提交
102 103 104 105 106 107 108 109

  clear() {
    super.clear();

    const data = this.chart.config.data;
    if (data.datasets.length) {
      const backgroundColor = data.datasets[0].backgroundColor;
      for (let i = 0; i < backgroundColor.length; i++) {
K
Kevin Nadro 已提交
110
        backgroundColor[i] = this.defaultColor;
J
Jason Park 已提交
111 112 113 114
      }
      this.chart.update();
    }
  }
J
Jason Park 已提交
115 116 117
}

const initView = (tracer) => {
J
Jason Park 已提交
118
  tracer.$wrapper = tracer.capsule.$wrapper = $('<canvas class="mchrt-chart">');
J
Jason Park 已提交
119
  tracer.$container.append(tracer.$wrapper);
J
Jason Park 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  tracer.chart = tracer.capsule.chart = new Chart(tracer.$wrapper, {
    type: 'bar',
    data: {
      labels: [],
      datasets: []
    },
    options: {
      scales: {
        yAxes: [{
          ticks: {
            beginAtZero: true
          }
        }]
      },
      animation: false,
      legend: false,
      responsive: true,
      maintainAspectRatio: false
    }
  });
J
Jason Park 已提交
140
};
N
nem035 已提交
141

142
module.exports = ChartTracer;