Commit 153945d0 authored by Joon Ahn's avatar Joon Ahn Committed by Commit Bot

Diagnostics: Change CPU Chart to an stacked area chart

https://screenshot.googleplex.com/5ywKyKorL3QKeCj

Bug:1125150
Test: browser_tests --gtest_filter=DiagnosticsApp*

Change-Id: I9c8641b7ddfeebefede0984fdeb0248d7bcec8c2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2477660Reviewed-by: default avatarZentaro Kavanagh <zentaro@chromium.org>
Commit-Queue: Joon Ahn <joonbug@chromium.org>
Cr-Commit-Position: refs/heads/master@{#818587}
parent 46b56128
...@@ -24,12 +24,12 @@ ...@@ -24,12 +24,12 @@
} }
/* TODO(joonbug): Update with real colors as vars */ /* TODO(joonbug): Update with real colors as vars */
.user-line { .user-area {
stroke: blue; fill: orange;
} }
.system-line { .system-area {
stroke: orange; fill: blue;
} }
</style> </style>
...@@ -44,8 +44,8 @@ ...@@ -44,8 +44,8 @@
<g id="gridLines" class="grid"></g> <g id="gridLines" class="grid"></g>
<g id="plotGroup" clip-path="url(#defClip)"> <g id="plotGroup" clip-path="url(#defClip)">
<path class="user-line"></path> <path class="user-area"></path>
<path class="system-line"></path> <path class="system-area"></path>
</g> </g>
</g> </g>
</svg> </svg>
......
...@@ -13,8 +13,8 @@ import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bun ...@@ -13,8 +13,8 @@ import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bun
/** /**
* @fileoverview * @fileoverview
* 'realtime-cpu-chart' is a moving line graph component used to display a * 'realtime-cpu-chart' is a moving stacked area graph component used to display
* realtime cpu usage information. * a realtime cpu usage information.
*/ */
Polymer({ Polymer({
is: 'realtime-cpu-chart', is: 'realtime-cpu-chart',
...@@ -169,48 +169,52 @@ Polymer({ ...@@ -169,48 +169,52 @@ Polymer({
// Feed data array to the plot group. // Feed data array to the plot group.
plotGroup.datum(this.data_); plotGroup.datum(this.data_);
// Select each line and configure the transition for animation. // Select each area and configure the transition for animation.
// d3.transition API @ https://github.com/d3/d3-transition#d3-transition. // d3.transition API @ https://github.com/d3/d3-transition#d3-transition.
// d3.easing API @ https://github.com/d3/d3-ease#api-reference. // d3.easing API @ https://github.com/d3/d3-ease#api-reference.
plotGroup.select('.user-line') plotGroup.select('.user-area')
.transition() .transition()
.duration(this.refreshInterval_) .duration(this.refreshInterval_)
.ease(d3.easeLinear) // Linear transition .ease(d3.easeLinear) // Linear transition
.on('start', this.drawChartLine_.bind(this, 'user-line')); .on('start', this.drawChartArea_.bind(this, 'user-area'));
plotGroup.select('.system-line') plotGroup.select('.system-area')
.transition() .transition()
.duration(this.refreshInterval_) .duration(this.refreshInterval_)
.ease(d3.easeLinear) // Linear transition .ease(d3.easeLinear) // Linear transition
.on('start', this.drawChartLine_.bind(this, 'system-line')); .on('start', this.drawChartArea_.bind(this, 'system-area'));
}, },
/** /**
* @param {string} lineClass class string for <path> element. * @param {string} areaClass class string for <path> element.
* @return {d3.Line} * @return {d3.Area}
* @private * @private
*/ */
getLineDefinition_(lineClass) { getAreaDefinition_(areaClass) {
return d3 return d3
.line() .area()
// Curved line // Curved area outline
.curve(d3.curveBasis) .curve(d3.curveBasis)
// Take the index of each data as x values. // Take the index of each data as x values.
.x((data, i) => this.xAxisScaleFn_(i)) .x((data, i) => this.xAxisScaleFn_(i))
// Pick system or user numbers to use as y values. // Bottom coordinates of each area.
.y(data => this.yAxisScaleFn_( .y0(data => this.yAxisScaleFn_(
lineClass === 'user-line' ? data.user : data.system)); areaClass === 'system-area' ? 0 : data.system))
// Top coordinates of each area.
.y1(data => this.yAxisScaleFn_(
areaClass === 'system-area' ? data.system :
data.system + data.user));
}, },
/** /**
* Takes a snapshot of current CPU readings and appends to the data array. * Takes a snapshot of current CPU readings and appends to the data array.
* This method is called by each line after each transition. * This method is called by each area after each transition.
* @private * @private
*/ */
appendDataSnapshot_() { appendDataSnapshot_() {
const now = new Date().getTime(); const now = new Date().getTime();
// We only want to append the data once per refreshInterval cycle even with // We only want to append the data once per refreshInterval cycle even with
// multiple lines. Roughly limit the call so that at least half of the // multiple areas. Roughly limit the call so that at least half of the
// refreshInterval has elapsed since the last update. // refreshInterval has elapsed since the last update.
if (now - this.dataLastUpdated_ > this.refreshInterval_ / 2) { if (now - this.dataLastUpdated_ > this.refreshInterval_ / 2) {
this.dataLastUpdated_ = now; this.dataLastUpdated_ = now;
...@@ -220,26 +224,26 @@ Polymer({ ...@@ -220,26 +224,26 @@ Polymer({
}, },
/** /**
* @param {string} lineClass class string for <path> element. * @param {string} areaClass class string for <path> element.
* @private * @private
*/ */
drawChartLine_(lineClass) { drawChartArea_(areaClass) {
this.appendDataSnapshot_(); this.appendDataSnapshot_();
const lineElement = assert(this.$$(`path.${lineClass}`)); const areaElement = assert(this.$$(`path.${areaClass}`));
// Reset the animation (transform) and redraw the line with new data. // Reset the animation (transform) and redraw the area with new data.
// this.data_ is already associated with the plotGroup, so no need to // this.data_ is already associated with the plotGroup, so no need to
// specify it directly here. // specify it directly here.
d3.select(lineElement) d3.select(areaElement)
.attr('d', this.getLineDefinition_(lineClass)) .attr('d', this.getAreaDefinition_(areaClass))
.attr('transform', null); .attr('transform', null);
// Start animation of the line towards left by one tick outside the chart // Start animation of the area towards left by one tick outside the chart
// boundary, then repeat the process. // boundary, then repeat the process.
d3.active(lineElement) d3.active(areaElement)
.attr('transform', 'translate(' + this.xAxisScaleFn_(-1) + ', 0)') .attr('transform', 'translate(' + this.xAxisScaleFn_(-1) + ', 0)')
.transition() .transition()
.on('start', this.drawChartLine_.bind(this, lineClass)); .on('start', this.drawChartArea_.bind(this, areaClass));
}, },
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment