-
Notifications
You must be signed in to change notification settings - Fork 12
Add Stat Spark Lines & Energy Mix Pie Chart #116
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
530d507
Hide Site & Source EUI behind <details>
vkoves 57f552f
Add test spark line
vkoves ba40da1
Make spark line look more like a spark line
vkoves 0158530
Show spark lines under each stat tile
vkoves 1fa5e8d
Finalize spark line styling and add units
vkoves 171765c
Clean up SparkLine code and improve styling
vkoves 1fbd1a8
Hide historical data table under technical info
vkoves afdf020
Fix mobile spark line spacing
vkoves afd2c6c
Add tooltip to spark line
vkoves b277579
Remove extra param
vkoves 487a8ce
Add energy mix pie chart
vkoves c259a3c
Tune pie chart for all electric buildings
vkoves d251fc9
Fix not allowing 100% Nat Gas pie chart
vkoves bb63dd4
Styling tweaks and fix showing NaN dots
vkoves 739298d
Ran yarn lint-fix
vkoves 8b41d67
Remove above or below avg triangle
vkoves b8ee9ca
Improve Spark line readability
vkoves 0b983cd
Improve stat tile responsiveness
vkoves d38ce0a
Add new graphs to Release Notes & tidy About
vkoves 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
This file contains hidden or 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 hidden or 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,151 @@ | ||
<template> | ||
<div class="spark-graph-cont"> | ||
<div | ||
class="label" | ||
v-html="graphTitle" | ||
/> | ||
|
||
<svg :id="'spark' + randomId"><!-- D3 inserts here --></svg> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { Component, Prop, Vue, Watch } from 'vue-property-decorator'; | ||
import * as d3 from 'd3'; | ||
|
||
export interface INumGraphPoint { | ||
x: number; | ||
y: number; | ||
} | ||
|
||
/** | ||
* A component that can graph an arbitrary array of numeric data as a spark line, a simple line | ||
* graph (like might be used in a news chevron for a stock) | ||
*/ | ||
@Component({}) | ||
export default class BarGraph extends Vue { | ||
@Prop({required: true}) graphTitle!: string; | ||
|
||
@Prop({required: true}) graphData!: Array<INumGraphPoint>; | ||
|
||
@Watch('graphData') | ||
onDataChanged(): void { | ||
this.renderGraph(); | ||
} | ||
|
||
/** Underlying size */ | ||
readonly width = 400; | ||
readonly height = 150; | ||
|
||
// The amount to shift the x-axis down by | ||
readonly xAxisOffset = 20; | ||
|
||
readonly graphMargins = { top: 30, right: 0, bottom: 80, left: 60 }; | ||
readonly barMargin = 0.2; | ||
|
||
randomId = Math.round(Math.random() * 1000); | ||
|
||
svg!: d3.Selection<SVGGElement, unknown, HTMLElement, any>; | ||
Check warningCode scanning / ESLint Disallow the `any` type Warning
Unexpected any. Specify a different type.
|
||
|
||
mounted(): void { | ||
const outerWidth = this.width + this.graphMargins.left + this.graphMargins.right; | ||
const outerHeight = this.height + this.graphMargins.top + this.graphMargins.bottom; | ||
|
||
this.svg = d3 | ||
.select("svg#spark" + this.randomId) | ||
.attr("width", outerWidth) | ||
.attr("height", outerHeight) | ||
.attr("viewBox", `0 0 ${outerWidth} ${outerHeight}`) | ||
.attr("preserveAspectRatio", "xMidYMid meet") | ||
.append("g") | ||
.attr("transform", `translate(${this.graphMargins.left},${this.graphMargins.top})`); | ||
|
||
this.renderGraph(); | ||
} | ||
|
||
renderGraph(): void { | ||
// Empty the SVG | ||
this.svg.html(null); | ||
|
||
// TODO: Fix passed years being strings and remove this conversion | ||
this.graphData.forEach((point: INumGraphPoint) => point.x = parseInt(point.x.toString())); | ||
|
||
const xVals: Array<number> = this.graphData.map((d) => d.x); | ||
const yVals: Array<number> = this.graphData.map((d) => d.y); | ||
|
||
const x = d3 | ||
.scaleLinear() | ||
.range([0, this.width]) | ||
.domain(d3.extent(xVals) as [number, number]) | ||
|
||
|
||
const y = d3 | ||
.scaleLinear() | ||
.domain([ d3.min(yVals) as number, d3.max(yVals) as number]) | ||
.rangeRound([this.height, 0]); | ||
|
||
// Render X axis | ||
this.svg.append("g") | ||
.attr('class', 'x-axis') | ||
.attr("transform", `translate(0, ${this.height + this.xAxisOffset})`) | ||
.call( | ||
d3.axisBottom(x) | ||
.tickFormat(d3.format('d')) | ||
// For spark line, only show first and last year (e.g. 2018 and 2022) | ||
.tickValues(d3.extent(xVals) as [number, number]) | ||
) | ||
|
||
.selectAll("text") | ||
.attr("transform", "translate(-10,0)rotate(-45)") | ||
.style("text-anchor", "end"); | ||
|
||
// Render Y axis | ||
this.svg.append("g") | ||
.attr('class', 'y-axis') | ||
.call( | ||
d3.axisLeft(y) | ||
.tickValues(d3.extent(yVals) as [number, number]) | ||
); | ||
|
||
|
||
// Add the line | ||
this.svg.append("path") | ||
.datum(this.graphData) | ||
.attr("fill", "none") | ||
.attr("stroke", "steelblue") | ||
.attr("stroke-width", 10) | ||
.attr("d", (d3.line() as any) | ||
|
||
.x((d: INumGraphPoint) => { return x(d.x as number) }) | ||
|
||
.y((d: INumGraphPoint) => { return y(d.y) }) | ||
|
||
) | ||
|
||
} | ||
|
||
} | ||
</script> | ||
|
||
<style lang="scss"> | ||
.spark-graph-cont { | ||
margin: 1rem 0; | ||
|
||
.label { | ||
font-weight: bold; | ||
font-size: 1.25rem; | ||
margin-bottom: 0.5rem; | ||
} | ||
|
||
svg { | ||
width: 100%; | ||
border: solid $border-thin $grey-light; | ||
aspect-ratio: 2; | ||
height: auto; | ||
max-width: 20rem; // 320px | ||
} | ||
|
||
.tick { | ||
font-weight: bold; | ||
font-size: 1.25rem; | ||
} | ||
|
||
// Hide tick lines on x-axis | ||
.tick line { display: none; } | ||
|
||
// Hide main y-axis line | ||
.y-axis .domain { display: none; } | ||
} | ||
</style> |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.