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

Cant provide x and y coordinates for bar data #2892

Closed
Montrazul opened this issue Jul 1, 2016 · 5 comments
Closed

Cant provide x and y coordinates for bar data #2892

Montrazul opened this issue Jul 1, 2016 · 5 comments

Comments

@Montrazul
Copy link

Montrazul commented Jul 1, 2016

Hello,

i wanted to create a combined chart using the latest version of chart.js (2.1.6) a line and a bar chart in the same chart. This is my code:

`

function initCombinedChart() {

    $("canvas").each(function() {
        var config = getConfigCombined($(this).attr("id"));
        var context = $(this);
        var combined = new Chart(context, config);
    });
}

function getConfigCombined(id) {
    var currentId = id;
    var currentIdNumber = currentId.substring((currentId.lastIndexOf("_") + 1), currentId.length);
    var entry = $("#" + id).data("entry");

    var labelMeasure = $("#evaluations_combined_measures").data("txt");
    var labelInsulin = $("#evaluations_combined_insulins").data("txt");

    var datasetLine = dataCombinedLine(labelMeasure, entry);
    var datasetCombined = dataCombinedBar(labelInsulin, entry);

    var config = {
        type: "bar",
        data: {
            labels: labelsFromEntry(entry),
            datasets: []
        },
        options: {
            responsive: true,
            title: {
                display: false
            },
            legend: {
                position: "bottom"
            },
            scales: {
                xAxes: [{
                    type: "time",
                    time: {
                        unit: "hour",
                        format: "HH:mm",
                        tooltipFormat: "HH:mm",
                        displayFormats: {
                            hour: "HH:mm",
                            day: "HH:mm",
                            week: "HH:mm",
                            month: "HH:mm",
                            quarter: "HH:mm",
                            year: "HH:mm"
                        }
                    },
                    gridLines : {
                        display : false
                    }
                }, ],
                yAxes: [{
                    type: "linear",
                    display: true,
                    position: "left",
                    id: "y-axis-0",
                    gridLines: {
                        show: true,
                    }
                }, {
                    type: "linear",
                    display: true,
                    position: "right",
                    id: "y-axis-1",
                    gridLines: {
                        show: false
                    }
                }]
            },
        }
    }

    if (datasetLine != null) {
        config.data.datasets.push(datasetLine);
    }

    if (datasetCombined != null) {
        config.data.datasets.push(datasetCombined);
    }

    return config;
}

function labelsFromEntry(entry) {
    var result = [];
    var entryCombined;
    var entryMeasure;
    var entryInsulin;

    if (entry.indexOf("-") >= 0) {
        entryCombined = entry.split("-");
        entryMeasure = entryCombined[0];
        entryInsulin = entryCombined[1];
    } else {
        entryMeasure = entry;
        entryInsulin = "";
    }

    var entryMeasureArray = entryMeasure.split(";");
    var entryInsulinArray = entryInsulin.split(";");

    entryMeasureArray.forEach(function(entry) {
        var entryPair = entry.split(",");
        var date = parseFloat(entryPair[0]);
        var dateFormat = moment(date).format("HH:mm");

        if (!result.includes(dateFormat)) {
            result.push(dateFormat);
        }
    });

    entryInsulinArray.forEach(function(entry) {
        var entryPair = entry.split(",");
        var date = parseFloat(entryPair[0]);
        var dateFormat = moment(date).format("HH:mm");

        if (!result.includes(dateFormat)) {
            result.push(dateFormat);
        }
    });

    return result;
}

function dataCombinedLine(label, entry) {
    var dataset = {
        type: "line",
        label: label,   
        lineTension: 0,
        backgroundColor: "#4078A7",
        borderCapStyle: "butt",
        borderJoinStyle: "miter",
        borderColor: "#4078A7",
        pointRadius: 5,
        pointBorderColor: "#4078A7",
        pointBackgroundColor: "#FFFFFF",
        pointBorderWidth: 3,
        pointHoverRadius: 5,
        pointHoverBackgroundColor: "#FFFFFF",
        pointHoverBorderWidth: 3,
        pointHitRadius: 5,
        data: dataCombinedLineFromEntries(entry),
        yAxisID : "y-axis-0",
        fill: false
    }

    return dataset;
}

function dataCombinedBar(label, entry) {
    var dataset = {
        type: "bar",
        label: label,
        backgroundColor: "#239471",
        borderCapStyle: "butt",
        borderJoinStyle: "miter",
        borderColor: "#239471",
        data: dataCombinedBarFromEntries(entry),
        yAxisID : "y-axis-1"
    }

    return dataset;
}

function dataCombinedLineFromEntries(entry) {
    var result = [];
    var entryMeasures = entry.split("-")[0];
    var entryMeasuresArray = entryMeasures.split(";");

    entryMeasuresArray.forEach(function(entry) {
        var entryPair = entry.split(",");
        var date = parseFloat(entryPair[0]);
        var value = entryPair[1];

        var data = {
            x: moment(date).format("HH:mm"),
            y: entryPair[1]
        }

        result.push(data);
    });

    return result;
}

function dataCombinedBarFromEntries(entry) {
    var result = [];

    if (entry.indexOf("-") >= 0) {
        var entryInsulins = entry.split("-")[1];
        var entryInsulinsArray = entryInsulins.split(";");

        entryInsulinsArray.forEach(function(entry) {
            var entryPair = entry.split(",");
            var date = parseFloat(entryPair[0]);
            var value = entryPair[1];

            var data = {
                x: moment(date).format("HH:mm"),
                y: entryPair[1]
            }

            result.push(entryPair[1]);
        });
    }

    return result;
}

`

As you can see for the line data im doing the following:

        var data = {
            x: moment(date).format("HH:mm"),
            y: entryPair[1]
        }

        result.push(data);

Unfortunately this is not possible for bar data. If i do this for bar data:

            var data = {
                x: moment(date).format("HH:mm"),
                y: entryPair[1]
            }

            result.push(data);

Instead of this:

            var data = {
                x: moment(date).format("HH:mm"),
                y: entryPair[1]
            }

            result.push(entryPair[1]);

The bar data is never showing up. Isnt there a similar functionality for bar data like it is for line data since there is no match between my line and my bar data. Just because i have a line data value for 08:00 o'clock that does not mean that there is a bar data value for 08:00 o'clock. And also there does not have to be a matching line data value for bar data.

@Montrazul Montrazul changed the title Uncaught TypeError: Cannot read property 'top' of undefined Cant provide x and y coordinates for bar data Jul 1, 2016
@etimberg
Copy link
Member

etimberg commented Jul 1, 2016

@Montrazul can you drop this in a fiddle?

@Montrazul
Copy link
Author

Montrazul commented Jul 1, 2016

Sure, https://jsfiddle.net/av15kuwj/6/

I just noticed that there are more things not correct. First thing is that beginAtZero: true seems to be ignored for the yAxes of the bar chart (you cant see the first bar). Second thing is, that the first and the last bar are overlapping with the yAxis

Here i used only y-value for the bar data. but as i said i would like to be able to use x and y values like i did for the line data so that the line and bar data are completely independent from each other.

Also the

gridLines: {
    show: false
}

for the bar data is ignored.

@JewelsJLF
Copy link
Contributor

JewelsJLF commented Mar 9, 2017

+1 I also would like to specify my bar and line chart data in sparse format: {x: value, y: value}

I'm already storing my data in sparse format and when it is updated the chart is not two-way bound b/c the data is an array of values rather than an array of objects which would be mapped to the source object.

@SesioN
Copy link

SesioN commented May 12, 2017

+1 👍

I've also ran into this issue, can have data from serval logfiles, where each can have a different log interval. Am displaying multiple graphs at the same time, which can be from several logfiles at once, meaning I need the possibility to have the data stored as with X / Y per entry.

So I tried using the sparse format but as it turns out it isn't supported for bars and so far I don't see any workaround for this issue?

@maxflex
Copy link

maxflex commented May 29, 2017

+1 👍

I have a perfectly working time scale line chart with X-Y data, and when I switch the type to bar – it breaks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

No branches or pull requests

5 participants