-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from brown-ccv/line-chart
feat: single line chart
- Loading branch information
Showing
5 changed files
with
242 additions
and
1 deletion.
There are no files selected for viewing
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,89 @@ | ||
<script> | ||
import vegaBaseMixin from '@/mixins/vega-base-mixin.js'; | ||
export default { | ||
mixins: [vegaBaseMixin], | ||
props: { | ||
x: { | ||
type: String, | ||
required: true, | ||
}, | ||
xLabel: { | ||
type: String, | ||
required: true, | ||
}, | ||
y: { | ||
type: String, | ||
required: true, | ||
}, | ||
yLabel: { | ||
type: String, | ||
required: true, | ||
}, | ||
}, | ||
computed: { | ||
baseSpec() { | ||
return { | ||
$schema: 'https://vega.github.io/schema/vega/v5.json', | ||
description: | ||
'A basic line chart example, with value labels shown upon mouse hover.', | ||
height: this.height, | ||
padding: 5, | ||
data: [ | ||
{ | ||
name: 'data', | ||
values: this.dataset, | ||
}, | ||
], | ||
scales: [ | ||
{ | ||
name: 'xscale', | ||
type: 'linear', | ||
domain: { data: 'data', field: this.x }, | ||
range: 'width', | ||
round: true, | ||
zero: false, | ||
}, | ||
{ | ||
name: 'yscale', | ||
type: 'linear', | ||
domain: { data: 'data', field: this.y }, | ||
nice: true, | ||
zero: false, | ||
range: 'height', | ||
}, | ||
], | ||
axes: [ | ||
{ | ||
orient: 'bottom', | ||
scale: 'xscale', | ||
title: this.xLabel, | ||
}, | ||
{ orient: 'left', scale: 'yscale', title: this.yLabel }, | ||
], | ||
marks: [ | ||
{ | ||
type: 'line', | ||
from: { data: 'data' }, | ||
encode: { | ||
enter: { | ||
x: { scale: 'xscale', field: this.x }, | ||
y: { scale: 'yscale', field: this.y }, | ||
strokeCap: { value: 'round' }, | ||
tooltip: { | ||
signal: `{ '${this.xLabel}': datum.${this.x}, '${this.yLabel}': datum.${this.y} }`, | ||
}, | ||
}, | ||
update: { | ||
interpolate: 'linear', | ||
defined: { signal: `isValid(datum.${this.y})` }, | ||
}, | ||
}, | ||
}, | ||
], | ||
}; | ||
}, | ||
}, | ||
}; | ||
</script> |
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,84 @@ | ||
import { Meta, ArgsTable, Story, Canvas } from '@storybook/addon-docs/blocks'; | ||
import DChartContainer from '../components/d-chart-container.vue'; | ||
import DLineChart from '../components/d-chart-line.vue'; | ||
|
||
<Meta | ||
title="Visualization/Charts/Line Chart" | ||
component={DLineChart} | ||
/> | ||
|
||
|
||
# Line Chart | ||
|
||
A Line Chart shows a variables over x and y coordinates. | ||
|
||
<ArgsTable story="Line Chart" /> | ||
|
||
## DLineChart component | ||
|
||
```html | ||
<DLineChart | ||
id="linechart" | ||
:dataset="[ | ||
{ month: 1, temp: 2, depth: 3}, | ||
{ month: 2, temp: 4, depth: 3.1}, | ||
{ month: 3, temp: 1, depth: 3.4}, | ||
]" | ||
:min-width="400" | ||
x="month" | ||
x-label="Month" | ||
y="depth" | ||
y-label="Water Depth" | ||
:include-actions="true" | ||
/> | ||
``` | ||
|
||
export const Template = (args, { argTypes }) => ({ | ||
props: Object.keys(argTypes), | ||
components: { DChartContainer, DLineChart }, | ||
template: ` | ||
<DChartContainer width="full"> | ||
<template #title>Line Chart</template> | ||
<template #description> | ||
A line chart shows the temp of observations at each month. | ||
</template | ||
> | ||
<template #chart> | ||
<DLineChart | ||
id="linechart" | ||
:dataset="dataset" | ||
:min-width="minWidth" | ||
:height="height" | ||
:x="x" | ||
:x-label="xLabel" | ||
:y="y" | ||
:y-label="yLabel" | ||
:include-actions="includeActions" | ||
:spec-override="specOverride" | ||
:enable-darkmode="enableDarkmode" | ||
/> | ||
</template> | ||
</DChartContainer> | ||
` | ||
}); | ||
|
||
<Canvas> | ||
<Story name="Line Chart" args={{ | ||
includeActions: true, | ||
minWidth: 400, | ||
height: 300, | ||
x: 'month', | ||
y: 'temp', | ||
xLabel: 'Month', | ||
yLabel: 'Temperature (C)', | ||
dataset: [ | ||
{ month: 1, temp: 2, depth: 3}, | ||
{ month: 2, temp: 4, depth: 3.1}, | ||
{ month: 3, temp: 1, depth: 3.4}, | ||
], | ||
specOverride: {axes: [{orient: 'top'}]}, | ||
enableDarkmode: true, | ||
}}> | ||
{Template.bind({})} | ||
</Story> | ||
</Canvas> |
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,67 @@ | ||
import { render, waitFor } from '@testing-library/vue'; | ||
import DLineChart from '@/components/d-chart-line.vue'; | ||
import '@testing-library/jest-dom'; | ||
import ResizeObserver from '../__mocks__/ResizeObserver'; // eslint-disable-line no-unused-vars | ||
|
||
let props = { | ||
id: 'line', | ||
minWidth: 400, | ||
height: 300, | ||
x: 'month', | ||
y: 'temp', | ||
xLabel: 'Month', | ||
yLabel: 'Temperature (C)', | ||
dataset: [ | ||
{ month: 1, temp: 2, depth: 3 }, | ||
{ month: 2, temp: 4, depth: 3.1 }, | ||
{ month: 3, temp: 1, depth: 3.4 }, | ||
], | ||
}; | ||
|
||
test('has id passed in props', async () => { | ||
const { container } = render(DLineChart, { props }); | ||
const main = container.firstElementChild; | ||
await waitFor(() => | ||
expect(main).toHaveAttribute('id', expect.stringContaining(props.id)) | ||
); | ||
}); | ||
|
||
test('has vizualization rendered', async () => { | ||
const { container, queryByText } = render(DLineChart, { props }); | ||
const main = container.firstElementChild; | ||
await waitFor(() => expect(main).not.toBeEmptyDOMElement()); | ||
|
||
// spot check props are passing through | ||
expect(queryByText(props.xLabel)).toBeInTheDocument(); | ||
expect(queryByText(props.yLabel)).toBeInTheDocument(); | ||
|
||
// actions are there | ||
expect(main).toHaveClass('has-actions'); | ||
}); | ||
|
||
test('can not include actions', async () => { | ||
const { container } = render(DLineChart, { | ||
props: { | ||
...props, | ||
includeActions: false, | ||
}, | ||
}); | ||
const main = container.firstElementChild; | ||
await waitFor(() => expect(main).not.toBeEmptyDOMElement()); | ||
|
||
expect(main).not.toHaveClass('has-actions'); | ||
}); | ||
|
||
test('can override spec', async () => { | ||
const { container, queryByText } = render(DLineChart, { | ||
props: { | ||
...props, | ||
specOverride: { axes: [{ title: 'Something else' }] }, | ||
}, | ||
}); | ||
const main = container.firstElementChild; | ||
await waitFor(() => expect(main).not.toBeEmptyDOMElement()); | ||
|
||
expect(queryByText('Something else')).toBeInTheDocument(); | ||
expect(queryByText(props.xLabel)).not.toBeInTheDocument(); | ||
}); |