-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial checkin of Metrics LineGraph Component #1098
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.line { | ||
fill: none; | ||
stroke-width: 1.5px; | ||
} | ||
|
||
.line0 { | ||
stroke:red; | ||
} | ||
|
||
.line1 { | ||
stroke:blue; | ||
} | ||
|
||
.axis path, | ||
.axis line { | ||
fill: none; | ||
stroke: #000; | ||
shape-rendering: crispEdges; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// source: components/metrics.ts | ||
/// <reference path="../typings/mithriljs/mithril.d.ts" /> | ||
/// <reference path="../typings/d3/d3.d.ts" /> | ||
/// <reference path="../models/timeseries.ts" /> | ||
|
||
/** | ||
* Components defines reusable components which may be used on multiple pages, | ||
* or multiple times on the same page. | ||
*/ | ||
module Components { | ||
/** | ||
* Metrics contains components used to display metrics data. | ||
*/ | ||
export module Metrics { | ||
/** | ||
* LineGraph displays a line graph of the data returned from a time | ||
* series query. | ||
*/ | ||
export module LineGraph { | ||
/** | ||
* ViewModel is the model for a specific LineGraph - in addition to | ||
* the backing Query object, it also maintains other per-component | ||
* display options. | ||
*/ | ||
interface ViewModel { | ||
width:number; | ||
height:number; | ||
query:Models.Metrics.Query; | ||
key?:number; | ||
} | ||
|
||
/** | ||
* Controller maintains the functionality needed for a single | ||
* LineGraph. | ||
*/ | ||
class Controller { | ||
margin = {top:20, right:20, bottom: 30, left:60}; | ||
|
||
// Width and height of the area containing the graph lines. | ||
private chartWidth: number; | ||
private chartHeight: number; | ||
|
||
// Scales and Axes. | ||
private timeScale = d3.time.scale(); | ||
private valScale = d3.scale.linear(); | ||
private timeAxis = d3.svg.axis().scale(this.timeScale).orient("bottom"); | ||
private valAxis = d3.svg.axis().scale(this.valScale).orient("left"); | ||
|
||
// Line data interpreter. | ||
private line = d3.svg.line() | ||
.x((d) => this.timeScale(d.timestamp_nanos/1.0e6)) | ||
.y((d) => this.valScale(d.value)); | ||
|
||
private color = d3.scale.category10(); | ||
|
||
// Query results. | ||
results:_mithril.MithrilPromise<Models.Metrics.QueryResultSet>; | ||
error = m.prop(""); | ||
|
||
constructor(public vm:ViewModel) { | ||
this.chartWidth = vm.width - this.margin.left - this.margin.right; | ||
this.chartHeight = vm.height - this.margin.top - this.margin.bottom; | ||
|
||
// Initialize axes. | ||
this.timeScale.range([0, this.chartWidth]); | ||
this.valScale.range([this.chartHeight, 0]); | ||
|
||
// Query for initial result set. | ||
this.results = vm.query.query().then(null, this.error); | ||
} | ||
|
||
/** drawGraph gets direct access to the svg element of the graph | ||
* after it is added to DOM. When the SVG element is first | ||
* initialized, we use D3 to draw the data from the query. | ||
*/ | ||
drawGraph = (element:Element, isInitialized:boolean, context:any) => { | ||
if (!isInitialized) { | ||
var data:Models.Metrics.QueryResult[] = this.results().results; | ||
|
||
// Scale value axis based on data. | ||
this.valScale.domain([ | ||
d3.min(data, (d) => d3.min(d.datapoints, (dp) => dp.value)), | ||
d3.max(data, (d) => d3.max(d.datapoints, (dp) => dp.value)) | ||
]); | ||
this.timeScale.domain([this.vm.query.start, this.vm.query.end]); | ||
|
||
var svg = d3.select(element) | ||
.attr("width", this.vm.width) | ||
.attr("height", this.vm.height) | ||
.append("g") | ||
.attr("transform", | ||
"translate(" + this.margin.left + "," + this.margin.top + ")"); | ||
svg.append("g") | ||
.attr("class", "x axis") | ||
.attr("transform", "translate(0," + this.chartHeight + ")") | ||
.call(this.timeAxis); | ||
svg.append("g") | ||
.attr("class", "y axis") | ||
.call(this.valAxis); | ||
|
||
// append lines | ||
svg.selectAll(".line") | ||
.data(data) | ||
.enter() | ||
.append("path") | ||
.attr("class", (d,i) => "line line" + i) | ||
.attr("d", (d) => this.line(d.datapoints)) | ||
.style("stroke", (d) => this.color(d.name)); | ||
} | ||
} | ||
} | ||
|
||
export function controller(model:ViewModel) { | ||
return new Controller(model); | ||
} | ||
|
||
export function view(ctrl) { | ||
if (ctrl.error()) { | ||
return m("", "error loading graph:" + ctrl.error()); | ||
} else if (ctrl.results()) { | ||
return m("svg.graph", {config: ctrl.drawGraph}); | ||
} else { | ||
return m("", "loading..."); | ||
} | ||
} | ||
|
||
/** | ||
* create instantiates a single instance of the LineGraph component | ||
* which displays data from the supplied Metrics.Query object. | ||
* | ||
* @param key The key param is used by mithril to track objects in lists which can be rearranged. | ||
*/ | ||
export function create(width:number, height:number, query:Models.Metrics.Query, key?:number){ | ||
var vm:ViewModel = {width:width, height:height, query:query} | ||
if (!!key) { | ||
vm.key = key; | ||
} | ||
return m.component(LineGraph, vm) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ugg... do you prefer some of these weird js operations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like !!