Alexander Farber
Alexander Farber

Reputation: 22988

How to make a SERIES line dashed in Google Line Chart?

In Google Line Chart how do you make a series line dashed?

For example the red line (called "Row B") in the screenshot below?

enter image description here

Below is my very simple test code, just open it in a browser and it will work instantly.

Please note that the usual suggestion to add certainty role:

    {"p":{"role":"certainty"},"label":"Dashed","type":"boolean"}

doesn't help here, because it would make dashed (parts of) the both lines (the rows "A" and "B").

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart']}]}"></script>
<script type="text/javascript">

        var data = {"rows":[
        {"c":[{"v":"C"},{"v":-43},{"v":-42}]},
        {"c":[{"v":"D"},{"v":-49},{"v":-39}]},
        {"c":[{"v":"E"},{"v":-49},{"v":-48}]},
        {"c":[{"v":"F"},{"v":-50},{"v":-49}]},
        {"c":[{"v":"G"},{"v":-57},{"v":-56}]}],

        "cols":[
        {"p":{"role":"domain"},"label":"MEASUREMENT","type":"string"},
        {"p":{"role":"data"},"label":"Row A","type":"number"},
        {"p":{"role":"data"},"label":"Row B","type":"number"}]};

        function drawCharts() {
            var x = new google.visualization.DataTable(data);

            var options = {
                title: 'How to make red line dashed?',
                width: 800,
                height: 600
            };

            var chart = new google.visualization.LineChart(document.getElementById('test'));
            chart.draw(x, options);
        }

        $(function() {
            google.setOnLoadCallback(drawCharts);
        });

</script>
</head>
<body>
<div id="test"></div>
</body>
</html>

Upvotes: 2

Views: 3835

Answers (1)

Sergey G
Sergey G

Reputation: 1221

Here is an example using the certainty role. Is there any reason why this doesn't work for you?

google.load('visualization', '1', {
    packages: ['corechart'],
    callback: drawVisualization
});

var data = {
    "rows": [
      {"c": [{"v": "C"}, {"v": -43}, {"v": -42}, {"v": false}]},
      {"c": [{"v": "D"}, {"v": -49}, {"v": -39}, {"v": false}]},
      {"c": [{"v": "E"}, {"v": -49}, {"v": -48}, {"v": false}]},
      {"c": [{"v": "F"}, {"v": -50}, {"v": -49}, {"v": false}]},
      {"c": [{"v": "G"}, {"v": -57}, {"v": -56}, {"v": false}]}],

    "cols": [
      {"p": {"role": "domain"},"label": "MEASUREMENT","type": "string"},
      {"p": {"role": "data"},"label": "Row A","type": "number"},
      {"p": {"role": "data"},"label": "Row B","type": "number"},
      {"p": {"role": "certainty"},"type": "boolean"}]
};

function drawVisualization() {
    var x = new google.visualization.DataTable(data);

    var options = {
        title: 'How to make red line dashed?',
        width: 800,
        height: 600
    };

    var chart = new google.visualization.LineChart(document.getElementById('visualization'));
    chart.draw(x, options);
}

Here is an example using the certainty role to make both lines dashed.

google.load('visualization', '1', {
    packages: ['corechart'],
    callback: drawVisualization
});

var data = {
    "rows": [
      {"c": [{"v": "C"}, {"v": -43}, {"v": false}, {"v": -42}, {"v": false}]},
      {"c": [{"v": "D"}, {"v": -49}, {"v": false}, {"v": -39}, {"v": false}]},
      {"c": [{"v": "E"}, {"v": -49}, {"v": false}, {"v": -48}, {"v": false}]},
      {"c": [{"v": "F"}, {"v": -50}, {"v": false}, {"v": -49}, {"v": false}]},
      {"c": [{"v": "G"}, {"v": -57}, {"v": false}, {"v": -56}, {"v": false}]}],

    "cols": [
      {"p": {"role": "domain"},"label": "MEASUREMENT","type": "string"},
      {"p": {"role": "data"},"label": "Row A","type": "number"},
      {"p": {"role": "certainty"},"type": "boolean"},
      {"p": {"role": "data"},"label": "Row B","type": "number"},
      {"p": {"role": "certainty"},"type": "boolean"}]
};

function drawVisualization() {
    var x = new google.visualization.DataTable(data);

    var options = {
        title: 'How to make red line dashed?',
        width: 800,
        height: 600
    };

    var chart = new google.visualization.LineChart(document.getElementById('visualization'));
    chart.draw(x, options);
}

Upvotes: 2

Related Questions