Giter Club home page Giter Club logo

Comments (2)

vasturiano avatar vasturiano commented on May 27, 2024

@IRISHMAN04 thanks for reaching out.

You should maintain a master data set from which you filter the subsets, because if you filter from the timelinesChart.data() getter you can't go back after toggling filters.

But if you do, and simply invoke .data(newData) after each filter interaction (no need for .refresh()) the component should function correctly. Please double-check that you are feeding the correct data object to the chart after each filter operation though.

Also you might want to set your own filtering interaction outside of the graph's own legend. The current legend is a simple item that wasn't designed to be interactive like this. You can hide it if you do .topMargin(0), and then replace it with your own above the chart.

from timelines-chart.

IRISHMAN04 avatar IRISHMAN04 commented on May 27, 2024

Thanks for the help, I'll make a seperate toggle box for it because I can't exactly do multi unit filtering with this. I did however figure out a way to get the onclicks to work so I've got that code here for anyone who wants it

/**
 * Setup the legend clicks.  
 * 
 * This has to run delayed after data enters the chart so that it happens after the legend has been created
 * 
 * @param {TimelinesChart} timelinesChart 
 * @param {HTMLElement} timelineDiv 
 * 
 */
function setupClicks(timelinesChart, timelineDiv) {
    const allColourSlots = timelineDiv.getElementsByClassName("color-slot");

    for (const slot of allColourSlots) {
        const rect = slot.getElementsByTagName("rect")[0]
        const text = slot.getElementsByTagName("text")[0]
        const myLambda = function () { filter_scale_unit(timelinesChart, text.innerHTML) };
        rect.addEventListener("click", myLambda);
        text.addEventListener("click", myLambda);
    }
}

/**
 * Filter the data based on the scale unit
 * 
 * @param {TimelinesChart} timelinesChart 
 * @param {string} scaleUnit 
 * 
 */
function filter_scale_unit(chart, scaleUnit) {
    const oldData = chart.data();
    const newData = JSON.parse(JSON.stringify(oldData));
    for (let i = newData.length - 1; i >= 0; i--) {
        let groupLength = 0
        for (const section of newData[i]["data"]) {
            for (let j = section["data"].length - 1; j >= 0; j--)
                if (section["data"][j]["val"] != scaleUnit)
                    section["data"].splice(j, 1);
            groupLength += section["data"].length
        }
        if (groupLength == 0)
            newData.splice(i, 1)
    }
    chart.data(newData);
}

The linchpin for me getting this to work correctly was making text a const, or else it would pass all my click handlers the text of the last item in my legend

from timelines-chart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.