Skip to content

Commit eaa7ac2

Browse files
committed
restores area charts for stocks
1 parent 0673f45 commit eaa7ac2

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

pages/examples/stocks/[ticker].tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as React from 'react';
22
import * as Server from '@common/server';
33
import * as Utilities from '@common/utilities';
44

5+
import AreaChart from '@root/system/graphs/AreaChart';
56
import AppLayout from '@system/layouts/AppLayout';
67
import Cookies from 'js-cookie';
7-
import LineChart from '@root/system/graphs/LineChart';
88
import GlobalModalManager from '@system/modals/GlobalModalManager';
99
import KeyHeader from '@system/KeyHeader';
1010
import Page from '@components/Page';
@@ -32,7 +32,7 @@ function ExampleStock(props) {
3232

3333
const isDataHydrated = stock && parsedData;
3434

35-
const tableHeadings = ['Object key', 'Object value'];
35+
const tableHeadings = ['key', 'value'];
3636
const tableData = Object.keys(stock).map((each) => {
3737
return {
3838
id: each,
@@ -77,7 +77,9 @@ function ExampleStock(props) {
7777
) : (
7878
<FormHeading style={{ marginTop: 48 }}>Access denied</FormHeading>
7979
)}
80-
{isDataHydrated ? <LineChart data={parsedData} style={{ marginTop: 32 }} /> : <FormParagraph>You must be signed in to view stock quotes</FormParagraph>}
80+
<br />
81+
<br />
82+
{isDataHydrated ? <AreaChart data={parsedData} /> : <FormParagraph>You must be signed in to view stock quotes</FormParagraph>}
8183
{isDataHydrated ? <Table data={tableData} headings={tableHeadings} style={{ marginTop: 24 }} /> : null}
8284
</ThinAppLayout>
8385
<GlobalModalManager viewer={props.viewer} />

system/graphs/LineChart.tsx

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import * as React from 'react';
12
import * as d3 from 'd3';
2-
import React, { useEffect, useRef, useState } from 'react';
33

44
const LineChart = (props) => {
5-
const d3Container = useRef<HTMLDivElement | null | any>(null);
6-
const [containerWidth, setContainerWidth] = useState(0);
5+
const d3Container = React.useRef<HTMLDivElement | null | any>(null);
6+
const [containerWidth, setContainerWidth] = React.useState(0);
77

88
const drawChart = (width) => {
99
if (!d3Container) {
@@ -14,7 +14,7 @@ const LineChart = (props) => {
1414
const svg = d3.select(d3Container.current);
1515
svg.selectAll('*').remove();
1616

17-
const margin = { top: 24, right: 48, bottom: 32, left: 48 };
17+
const margin = { top: 16, right: 0, bottom: 32, left: 32 };
1818
const height = +svg.attr('height') - margin.top - margin.bottom;
1919
const drawWidth = width - margin.left - margin.right;
2020

@@ -45,12 +45,12 @@ const LineChart = (props) => {
4545
.domain([0, d3.max(props.data, (d) => d.value)])
4646
.range([height, 0]);
4747

48-
const yAxis = g.append('g').call(d3.axisLeft(yScale).tickSize(-drawWidth).tickPadding(8));
48+
const yAxis = g.append('g').call(d3.axisLeft(yScale).ticks(6).tickSize(-drawWidth).tickPadding(8));
4949
yAxis.attr('shape-rendering', 'crispEdges').selectAll('.tick text').style('font-size', 'var(--type-scale-fixed-small)').style('fill', 'var(--theme-border)');
5050
yAxis.selectAll('.domain, .tick line').attr('stroke', 'var(--theme-border)');
5151
yAxis.selectAll('.domain').remove();
5252

53-
const xAxis = g.append('g').attr('transform', `translate(0,${height})`).call(d3.axisBottom(xScale));
53+
const xAxis = g.append('g').attr('transform', `translate(0,${height})`).call(d3.axisBottom(xScale).ticks(6));
5454
xAxis.selectAll('.tick text').style('fill', 'var(--theme-border)').style('font-size', 'var(--type-scale-fixed-small)');
5555
xAxis.selectAll('.tick line, .domain').style('stroke', 'var(--theme-border)');
5656
xAxis.selectAll('.domain').remove();
@@ -117,10 +117,8 @@ const LineChart = (props) => {
117117
const yUpper = yScale(d.upper_ci);
118118
const yMidpoint = (yLower + yUpper) / 2;
119119

120-
// Midpoint
121120
g.append('circle').attr('cx', x).attr('cy', yMidpoint).attr('r', 3).attr('fill', color);
122121

123-
// Midpoint label
124122
g.append('text')
125123
.attr('x', x + 5)
126124
.attr('y', yMidpoint)
@@ -149,7 +147,7 @@ const LineChart = (props) => {
149147
});
150148
}
151149

152-
useEffect(() => {
150+
React.useEffect(() => {
153151
if (!d3Container || !d3Container.current) {
154152
return;
155153
}
@@ -167,7 +165,7 @@ const LineChart = (props) => {
167165
return () => window.removeEventListener('resize', handleResize);
168166
}, [props.data]);
169167

170-
useEffect(() => {
168+
React.useEffect(() => {
171169
drawChart(containerWidth);
172170
}, [containerWidth, props.data, props.showErrorBars]);
173171

0 commit comments

Comments
 (0)