Skip to content

Commit 4956069

Browse files
author
Pete Richards
committed
[Reorg] Make timeline-specific chart directive
Make a separate chart directive for drawing resource graphs in timelines. This is in preparation for a new plot bundle which will make a large number of changes to the drawing API to support newly requested features. By separating code, there will be no impact to the timeline when the new plot features are added.
1 parent d94e8b1 commit 4956069

File tree

12 files changed

+1033
-40
lines changed

12 files changed

+1033
-40
lines changed

docs/src/guide/index.md

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,41 +1339,6 @@ are supported:
13391339
Open MCT defines several Angular directives that are intended for use both
13401340
internally within the platform, and by plugins.
13411341

1342-
## Chart
1343-
1344-
The `mct-chart` directive is used to support drawing of simple charts. It is
1345-
present to support the Plot view, and its functionality is limited to the
1346-
functionality that is relevant for that view.
1347-
1348-
This directive is used at the element level and takes one attribute, `draw`
1349-
which is an Angular expression which will should evaluate to a drawing object.
1350-
This drawing object should contain the following properties:
1351-
1352-
* `dimensions`: The size, in logical coordinates, of the chart area. A
1353-
two-element array or numbers.
1354-
* `origin`: The position, in logical coordinates, of the lower-left corner of
1355-
the chart area. A two-element array or numbers.
1356-
* `lines`: An array of lines (e.g. as a plot line) to draw, where each line is
1357-
expressed as an object containing:
1358-
* `buffer`: A Float32Array containing points in the line, in logical
1359-
coordinates, in sequential x,y pairs.
1360-
* `color`: The color of the line, as a four-element RGBA array, where
1361-
each element is a number in the range of 0.0-1.0.
1362-
* `points`: The number of points in the line.
1363-
* `boxes`: An array of rectangles to draw in the chart area. Each is an object
1364-
containing:
1365-
* `start`: The first corner of the rectangle, as a two-element array of
1366-
numbers, in logical coordinates.
1367-
* `end`: The opposite corner of the rectangle, as a two-element array of
1368-
numbers, in logical coordinates. color : The color of the line, as a
1369-
four-element RGBA array, where each element is a number in the range of
1370-
0.0-1.0.
1371-
1372-
While `mct-chart` is intended to support plots specifically, it does perform
1373-
some useful management of canvas objects (e.g. choosing between WebGL and Canvas
1374-
2D APIs for drawing based on browser support) so its usage is recommended when
1375-
its supported drawing primitives are sufficient for other charting tasks.
1376-
13771342
## Container
13781343

13791344
The `mct-container` is similar to the `mct-include` directive insofar as it allows

platform/features/plot/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Plot README
2+
3+
## Chart
4+
5+
The `mct-chart` directive is used to support drawing of simple charts. It is
6+
present to support the Plot view, and its functionality is limited to the
7+
functionality that is relevant for that view.
8+
9+
This directive is used at the element level and takes one attribute, `draw`
10+
which is an Angular expression which will should evaluate to a drawing object.
11+
This drawing object should contain the following properties:
12+
13+
* `dimensions`: The size, in logical coordinates, of the chart area. A
14+
two-element array or numbers.
15+
* `origin`: The position, in logical coordinates, of the lower-left corner of
16+
the chart area. A two-element array or numbers.
17+
* `lines`: An array of lines (e.g. as a plot line) to draw, where each line is
18+
expressed as an object containing:
19+
* `buffer`: A Float32Array containing points in the line, in logical
20+
coordinates, in sequential x,y pairs.
21+
* `color`: The color of the line, as a four-element RGBA array, where
22+
each element is a number in the range of 0.0-1.0.
23+
* `points`: The number of points in the line.
24+
* `boxes`: An array of rectangles to draw in the chart area. Each is an object
25+
containing:
26+
* `start`: The first corner of the rectangle, as a two-element array of
27+
numbers, in logical coordinates.
28+
* `end`: The opposite corner of the rectangle, as a two-element array of
29+
numbers, in logical coordinates. color : The color of the line, as a
30+
four-element RGBA array, where each element is a number in the range of
31+
0.0-1.0.
32+
33+
While `mct-chart` is intended to support plots specifically, it does perform
34+
some useful management of canvas objects (e.g. choosing between WebGL and Canvas
35+
2D APIs for drawing based on browser support) so its usage is recommended when
36+
its supported drawing primitives are sufficient for other charting tasks.
37+

platform/features/timeline/bundle.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ define([
3838
"./src/directives/MCTSwimlaneDrop",
3939
"./src/directives/MCTSwimlaneDrag",
4040
"./src/services/ObjectLoader",
41+
"./src/chart/MCTTimelineChart",
4142
"text!./res/templates/values.html",
4243
"text!./res/templates/timeline.html",
4344
"text!./res/templates/activity-gantt.html",
@@ -67,6 +68,7 @@ define([
6768
MCTSwimlaneDrop,
6869
MCTSwimlaneDrag,
6970
ObjectLoader,
71+
MCTTimelineChart,
7072
valuesTemplate,
7173
timelineTemplate,
7274
activityGanttTemplate,
@@ -556,6 +558,14 @@ define([
556558
"depends": [
557559
"dndService"
558560
]
561+
},
562+
{
563+
"key": "mctTimelineChart",
564+
"implementation": MCTTimelineChart,
565+
"depends": [
566+
"$interval",
567+
"$log"
568+
]
559569
}
560570
],
561571
"services": [

platform/features/timeline/res/templates/resource-graphs.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<span ng-controller="TimelineGraphController as graphController">
2323
<div class="t-graph l-graph" ng-repeat="graph in parameters.graphs">
2424
<div class="l-graph-area l-canvas-holder">
25-
<mct-chart draw="graph.drawingObject"></mct-chart>
25+
<mct-timeline-chart draw="graph.drawingObject"></mct-timeline-chart>
2626
</div>
2727
<div class="t-graph-labels l-graph-labels">
2828
<mct-include key="'timeline-resource-graph-labels'"
@@ -31,4 +31,4 @@
3131
</mct-include>
3232
</div>
3333
</div>
34-
</span>
34+
</span>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*****************************************************************************
2+
* Open MCT, Copyright (c) 2014-2016, United States Government
3+
* as represented by the Administrator of the National Aeronautics and Space
4+
* Administration. All rights reserved.
5+
*
6+
* Open MCT is licensed under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0.
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations
15+
* under the License.
16+
*
17+
* Open MCT includes source code licensed under additional open source
18+
* licenses. See the Open Source Licenses file (LICENSES.md) included with
19+
* this source code distribution or the Licensing information page available
20+
* at runtime from the About dialog for additional information.
21+
*****************************************************************************/
22+
23+
define(
24+
[],
25+
function () {
26+
27+
/**
28+
* Create a new chart which uses Canvas's 2D API for rendering.
29+
*
30+
* @memberof platform/features/plot
31+
* @constructor
32+
* @implements {platform/features/plot.Chart}
33+
* @param {CanvasElement} canvas the canvas object to render upon
34+
* @throws {Error} an error is thrown if Canvas's 2D API is unavailable.
35+
*/
36+
function Canvas2DChart(canvas) {
37+
this.canvas = canvas;
38+
this.c2d = canvas.getContext('2d');
39+
this.width = canvas.width;
40+
this.height = canvas.height;
41+
this.dimensions = [this.width, this.height];
42+
this.origin = [0, 0];
43+
44+
if (!this.c2d) {
45+
throw new Error("Canvas 2d API unavailable.");
46+
}
47+
}
48+
49+
// Convert from logical to physical x coordinates
50+
Canvas2DChart.prototype.x = function (v) {
51+
return ((v - this.origin[0]) / this.dimensions[0]) * this.width;
52+
};
53+
54+
// Convert from logical to physical y coordinates
55+
Canvas2DChart.prototype.y = function (v) {
56+
return this.height -
57+
((v - this.origin[1]) / this.dimensions[1]) * this.height;
58+
};
59+
60+
// Set the color to be used for drawing operations
61+
Canvas2DChart.prototype.setColor = function (color) {
62+
var mappedColor = color.map(function (c, i) {
63+
return i < 3 ? Math.floor(c * 255) : (c);
64+
}).join(',');
65+
this.c2d.strokeStyle = "rgba(" + mappedColor + ")";
66+
this.c2d.fillStyle = "rgba(" + mappedColor + ")";
67+
};
68+
69+
70+
Canvas2DChart.prototype.clear = function () {
71+
var canvas = this.canvas;
72+
this.width = canvas.width;
73+
this.height = canvas.height;
74+
this.c2d.clearRect(0, 0, this.width, this.height);
75+
};
76+
77+
Canvas2DChart.prototype.setDimensions = function (newDimensions, newOrigin) {
78+
this.dimensions = newDimensions;
79+
this.origin = newOrigin;
80+
};
81+
82+
Canvas2DChart.prototype.drawLine = function (buf, color, points) {
83+
var i;
84+
85+
this.setColor(color);
86+
87+
// Configure context to draw two-pixel-thick lines
88+
this.c2d.lineWidth = 2;
89+
90+
// Start a new path...
91+
if (buf.length > 1) {
92+
this.c2d.beginPath();
93+
this.c2d.moveTo(this.x(buf[0]), this.y(buf[1]));
94+
}
95+
96+
// ...and add points to it...
97+
for (i = 2; i < points * 2; i = i + 2) {
98+
this.c2d.lineTo(this.x(buf[i]), this.y(buf[i + 1]));
99+
}
100+
101+
// ...before finally drawing it.
102+
this.c2d.stroke();
103+
};
104+
105+
Canvas2DChart.prototype.drawSquare = function (min, max, color) {
106+
var x1 = this.x(min[0]),
107+
y1 = this.y(min[1]),
108+
w = this.x(max[0]) - x1,
109+
h = this.y(max[1]) - y1;
110+
111+
this.setColor(color);
112+
this.c2d.fillRect(x1, y1, w, h);
113+
};
114+
115+
return Canvas2DChart;
116+
}
117+
);

0 commit comments

Comments
 (0)