Skip to content
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

How to add axes and grid to webgl-candlesticks example? #1772

Open
YVin3D opened this issue Nov 17, 2022 · 1 comment
Open

How to add axes and grid to webgl-candlesticks example? #1772

YVin3D opened this issue Nov 17, 2022 · 1 comment
Milestone

Comments

@YVin3D
Copy link

YVin3D commented Nov 17, 2022

I'm using the WebGL candlesticks example and trying to add and gridlines axes to it like in this tutorial or this chart

primarily using this code snippet:

const pointSeries = fc
  .seriesWebglPoint()
  .crossValue(d => d.x)
  .mainValue(d => d.y);

const chart = fc
  .chartCartesian(xScale, yScale)
  .webglPlotArea(pointSeries)

d3.select("#chart")
    .datum(data)
    .call(chart);

the problem is that the candlesticks examples uses a container object to run the select on. I haven't found any references for container in the d3fc docs.

here is the code snippet for rendering the candlesticks:

d3.select(container)
    .on('click', () => {
        const domain = xScale.domain();
        const mid = (domain[1].valueOf() - domain[0].valueOf()) / 2;
        xScale.domain([domain[0], new Date(domain[1].valueOf() - mid)]);
        series.bandwidth(series.bandwidth()() * 2);
        container.requestRedraw();
    })
    .on('measure', event => {
        const { width, height } = event.detail;
        xScale.range([0, width]);
        yScale.range([height, 0]);

        gl = container.querySelector('canvas').getContext('webgl');
        series.context(gl);
    })
    .on('draw', () => {
        if (pixels == null) {
            pixels = new Uint8Array(
                gl.drawingBufferWidth * gl.drawingBufferHeight * 4
            );
        }
        performance.mark(`draw-start-${frame}`);
        series(data);
        // Force GPU to complete rendering to allow accurate performance measurements to be taken
        gl.readPixels(
            0,
            0,
            gl.drawingBufferWidth,
            gl.drawingBufferHeight,
            gl.RGBA,
            gl.UNSIGNED_BYTE,
            pixels
        );
        performance.measure(`draw-duration-${frame}`, `draw-start-${frame}`);
        frame++;
    });

container.requestRedraw();

TLDR: How do I combine the two code snippets? Also, can anyone guide me to resources on how to learn more about this part of the code?

Thanks!

@YVin3D
Copy link
Author

YVin3D commented Nov 17, 2022

Fixed, but now the commented out clause around the "draw" callback makes the graph disappear:

const chart = fc
  .chartCartesian(xScale, yScale)
  .canvasPlotArea(gridline)
  .webglPlotArea(candlesticks)
  .decorate((sel) => {
            sel
            .enter()
            .select('.webgl-plot-area')
            .raise()
            .on('measure', event => {
              const { width, height } = event.detail;
                xScale.range([0, width]);
                yScale.range([height, 0]);

                gl = container.querySelector('canvas').getContext('webgl');
                candlesticks.context(gl);
           })
           .on('click', () => {
            const domain = xScale.domain();
            const mid = (domain[1].valueOf() - domain[0].valueOf()) / 2;
            xScale.domain([domain[0], new Date(domain[1].valueOf() - mid)]);
            candlesticks.bandwidth(candlesticks.bandwidth()() * 2);
            container.requestRedraw();
           })
           .on('measure.bandwidth', (event, d) => { // statically updates bandwidth measure on screen stretch
                const { width } = event.detail;
                candlesticks.bandwidth(width/100.0);
           })
//           .on('draw', () => {
//               if (pixels == null) {
//                   pixels = new Uint8Array(
//                       gl.drawingBufferWidth * gl.drawingBufferHeight * 4
//                   );
//               }
//               performance.mark(`draw-start-${frame}`);
//               candlesticks(data);
//               // Force GPU to complete rendering to allow accurate performance measurements to be taken
//               gl.readPixels(
//                   0,
//                   0,
//                   gl.drawingBufferWidth,
//                   gl.drawingBufferHeight,
//                   gl.RGBA,
//                   gl.UNSIGNED_BYTE,
//                   pixels
//               );
//               performance.measure(`draw-duration-${frame}`, `draw-start-${frame}`);
//               frame++;
//           })
  })

d3.select('#chart')
    .datum(data)
    .call(chart);

container.requestRedraw();

@cfisher-scottlogic cfisher-scottlogic added this to the Triage issues milestone Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants