【发布时间】:2020-08-29 18:37:26
【问题描述】:
我是 D3 的新手,正在尝试创建一个能够添加新数据和更新图表的折线图。到目前为止,我有这段代码能够在给定数据数组的情况下绘制静态图表:
import * as d3 from 'd3';
import CreateData from './CreateData';
import { svg } from 'd3';
const MARGIN = {TOP: 50, BOTTOM: 90, LEFT: 75, RIGHT: 50};
const WIDTH = window.innerWidth - MARGIN.LEFT - MARGIN.RIGHT;
const HEIGHT = window.innerHeight - MARGIN.TOP - MARGIN.BOTTOM;
const duration = 500;
const max = 10;
const step = 10;
const dataClass = new CreateData(10);
let data = (dataClass).createTimeData(); /*Data aray containing 10 elements (each element is {time: DateObject, value: "Random value between 0, 10"})*/
let minData = d3.min(data, function(c) { return c.time});
export default class LiveD3Chart {
// element is the parent DOM element where you want data to be displayed
constructor (element) {
console.log(data);
this.svg = this.getSvgElement(element);
this.xScale = this.getScaleX();
this.yScale = this.getScaleY();
this.xAxis = this.getAxisX();
this.yAxis = this.getAxisY();
this.line = this.getLine();
this.chartGroup = this.svg.append('g');
this.path = this.chartGroup.append('path');
}
getSvgElement(parent) {
const svg = (d3.select(parent)).append('svg')
.attr('width', WIDTH + MARGIN.LEFT + MARGIN.RIGHT)
.attr('height', HEIGHT + MARGIN.TOP + MARGIN.BOTTOM)
.append('g')
.attr('transform', `translate(${MARGIN.LEFT}, ${MARGIN.TOP})`);
return svg;
}
getScaleX() {
const xScale = d3.scaleTime()
.domain(d3.extent(data, function(d) {return (d.time).getTime() /*- (500 * 2)*/}))
.range([0, WIDTH]);
return xScale;
}
getScaleY() {
const yScale = d3.scaleLinear()
.domain([0, max])
.range([HEIGHT, 0]);
return yScale;
}
getAxisX() {
const xAxis = this.svg.append('g')
.attr('class', 'x-axis')
.attr('transform', `translate(0, ${HEIGHT})`)
.call(d3.axisBottom(this.xScale));
return xAxis;
}
getAxisY() {
const yAxis = this.svg.append('g')
.attr('class', 'y-axis')
.call(d3.axisLeft(this.yScale));
return yAxis;
}
getLine() {
const line = d3.line()
.x((d) => this.xScale(d.time))
.y(d => this.yScale(d.data));
return line;
}
run() {
console.log(d3.min(data, function(c) { return c.time}));
this.path.datum(data)
.attr('class', 'data-line glowed')
.style('stroke', '#D073BA')
.style('stroke-width', 2)
.style('fill', 'none')
.attr('transform', null)
.attr('d', this.line);
this.svg.append('text')
.attr("transform", `translate(${WIDTH / 2} , ${HEIGHT + MARGIN.TOP})`)
.style('text-anchor', 'middle')
.text('Label X');
this.svg.append('text')
.attr('transform', function() {
return `translate(${-MARGIN.LEFT / 2}, ${HEIGHT / 2}) rotate(-90)`;
})
.style('text-anchor', 'middle')
.text('Values');
const circles = this.svg.selectAll('circle')
.data(data)
.enter()
.append('circle')
.attr('class', 'circle')
.attr('cx', (d, i) => this.xScale(d.time))
.attr('cy', d => this.yScale(d.data))
.attr('r', 4)
.style('fill', '#D073BA')
.style('stroke', '#11141C')
.style('stroke-width', 2);
}
输出图表示例:
按照此处的示例:http://bl.ocks.org/Sohalt/9715be30ba57e00f2275d49247fa7118/43a24a4dfa44738a58788d05230407294ab7a348,我试图实现实时功能。更具体地说,我无法理解这部分的目的:“x.domain([globalX - (max - step), globalX]);”以及在我的情况下将如何实施。
对于此事的任何帮助将不胜感激。
【问题讨论】:
标签: javascript d3.js