Skip to content

Commit e6a4a09

Browse files
committed
optimize chart
1 parent d99cc02 commit e6a4a09

File tree

6 files changed

+119
-62
lines changed

6 files changed

+119
-62
lines changed

js/module/tracer/chart.js

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,28 @@ class ChartTracer extends Tracer {
88
constructor(name) {
99
super(name);
1010

11+
this.color = {
12+
selected: 'rgba(255, 0, 0, 1)',
13+
notified: 'rgba(0, 0, 255, 1)',
14+
default: 'rgba(136, 136, 136, 1)'
15+
};
16+
1117
if (this.isNew) initView(this);
1218
}
1319

1420
setData(C) {
1521
if (super.setData.apply(this, arguments)) return true;
1622

17-
if (this.chart) this.chart.destroy();
1823
var color = [];
19-
for (var i = 0; i < C.length; i++) color.push('rgba(136, 136, 136, 1)');
20-
var data = {
21-
type: 'bar',
22-
data: {
23-
labels: C.map(String),
24-
datasets: [{
25-
backgroundColor: color,
26-
data: C
27-
}]
28-
},
29-
options: {
30-
scales: {
31-
yAxes: [{
32-
ticks: {
33-
beginAtZero: true
34-
}
35-
}]
36-
},
37-
animation: false,
38-
legend: false,
39-
responsive: true,
40-
maintainAspectRatio: false
41-
}
24+
for (var i = 0; i < C.length; i++) color.push(this.color.default);
25+
this.chart.config.data = {
26+
labels: C.map(String),
27+
datasets: [{
28+
backgroundColor: color,
29+
data: C
30+
}]
4231
};
43-
this.chart = this.capsule.chart = new Chart(this.$wrapper, data);
32+
this.chart.update();
4433
}
4534

4635
_notify(s, v) {
@@ -87,9 +76,8 @@ class ChartTracer extends Tracer {
8776
}
8877
case 'denotify':
8978
case 'deselect':
90-
var color = step.type == 'denotify' || step.type == 'deselect' ? 'rgba(136, 136, 136, 1)' : 'rgba(255, 0, 0, 1)';
9179
case 'select':
92-
if (color === undefined) var color = 'rgba(0, 0, 255, 1)';
80+
let color = step.type == 'notify' ? this.color.notified : step.type == 'select' ? this.color.selected : this.color.default;
9381
if (step.e !== undefined)
9482
for (var i = step.s; i <= step.e; i++)
9583
this.chart.config.data.datasets[0].backgroundColor[i] = color;
@@ -107,11 +95,44 @@ class ChartTracer extends Tracer {
10795

10896
this.chart.resize();
10997
}
98+
99+
clear() {
100+
super.clear();
101+
102+
const data = this.chart.config.data;
103+
if (data.datasets.length) {
104+
const backgroundColor = data.datasets[0].backgroundColor;
105+
for (let i = 0; i < backgroundColor.length; i++) {
106+
backgroundColor[i] = this.color.default;
107+
}
108+
this.chart.update();
109+
}
110+
}
110111
}
111112

112113
const initView = (tracer) => {
113114
tracer.$wrapper = tracer.capsule.$wrapper = $('<canvas class="mchrt-chart">');
114115
tracer.$container.append(tracer.$wrapper);
116+
tracer.chart = tracer.capsule.chart = new Chart(tracer.$wrapper, {
117+
type: 'bar',
118+
data: {
119+
labels: [],
120+
datasets: []
121+
},
122+
options: {
123+
scales: {
124+
yAxes: [{
125+
ticks: {
126+
beginAtZero: true
127+
}
128+
}]
129+
},
130+
animation: false,
131+
legend: false,
132+
responsive: true,
133+
maintainAspectRatio: false
134+
}
135+
});
115136
};
116137

117138
module.exports = ChartTracer;

public/algorithm_visualizer.js

Lines changed: 65 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/algorithm_visualizer.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/algorithm_visualizer.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/algorithm_visualizer.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/algorithm_visualizer.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)