I am currently working on integrating a d3js V4 chart into an Angular 4 application. The chart is designed to show multiple sets of data as individual lines.
One issue I am facing is getting the zoom feature to function correctly, specifically on the X-axis only.
Within the zoomed() function, I have attempted various approaches with comments outlining why they are not successful.
createChart() {
const element = this.chartContainer.nativeElement;
this.width = element.offsetWidth - this.margin.left - this.margin.right;
this.height = element.offsetHeight - this.margin.top - this.margin.bottom;
// canvas
const svg = d3.select(element).append('svg')
.attr('width', element.offsetWidth)
.attr('height', element.offsetHeight);
// chart plot area
this.chart = svg.append('g')
.attr('transform', 'translate(' + this.margin.left + ',' + this.margin.top + ')');
// domains
const xDomain = [this.from, this.to];
const yDomain = [0, 100];
// scales
this.xScale = d3.scaleTime().domain(xDomain).range([0, this.width]);
this.yScale = d3.scaleLinear().domain(yDomain).range([this.height, 0]);
// "data" function to draw lines
this.valueline = d3.line<TimeValueObject>()
.curve(d3.curveStepBefore)
.x((d) => this.xScale(d.time))
.y((d) => this.yScale(d.value));
// X Axis
this.xAxis = this.chart.append('g')
.attr('class', 'xaxis')
.attr('transform', 'translate(0,' + this.height + ')')
.call(d3.axisBottom(this.xScale)
.ticks(this.xTicks)
.tickFormat(function (d) { return moment.utc(d).format(this.timeFormat); }));
// Y Axis
this.yAxis = this.chart.append('g')
.attr('class', 'yaxis')
.call(d3.axisLeft(this.yScale)
.ticks(this.yTicks));
// zoom
const zoom = d3.zoom()
.on('zoom', this.zoomed.bind(this));
this.chart.call(zoom);
}
addData(myDataObject) {
// Add a data valueline path
this.chart.append('path')
.attr('class', 'line')
.attr('stroke', myDataObject.strokeStyle)
.attr('id', myDataObject.name)
.attr('d', this.valueline(myDataObject.data));
}
zoomed() {
// Multiple attempts at implementing zoom functionality.
// Based on examples from:
// <a href="https://github.com/d3/d3-zoom" rel="nofollow noreferrer">https://github.com/d3/d3-zoom</a>
}
The above code snippets reflect my efforts in aligning the zoom behavior with my specific requirements within the charting implementation.