Skip to content

Commit

Permalink
Hide legend only when data given in single series format
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed May 15, 2013
1 parent b1fd553 commit f5442e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
15 changes: 10 additions & 5 deletions chartkick.js
Expand Up @@ -96,7 +96,8 @@
var options = clone(defaultOptions);

// hide legend
if (series.length === 1) {
// this is *not* an external option!
if (opts.hideLegend) {
hideLegend(options);
}

Expand Down Expand Up @@ -469,12 +470,16 @@
return a[0].getTime() - b[0].getTime();
}

function processSeries(series, time) {
function processSeries(series, opts, time) {
var i, j, data, r, key;

// see if one series or multiple
if (!isArray(series) || typeof series[0] !== "object" || isArray(series[0])) {
series = [{name: "Value", data: series}];
opts.hideLegend = true;
}
else {
opts.hideLegend = false;
}

// right format
Expand All @@ -496,11 +501,11 @@
}

function processLineData(element, data, opts) {
renderLineChart(element, processSeries(data, true), opts);
renderLineChart(element, processSeries(data, opts, true), opts);
}

function processColumnData(element, data, opts) {
renderColumnChart(element, processSeries(data, false), opts);
renderColumnChart(element, processSeries(data, opts, false), opts);
}

function processPieData(element, data, opts) {
Expand All @@ -515,7 +520,7 @@
if (typeof element === "string") {
element = document.getElementById(element);
}
fetchDataSource(element, data, opts || {}, callback);
fetchDataSource(element, data, clone(opts || {}), callback);
}

// define classes
Expand Down
6 changes: 6 additions & 0 deletions examples/index.html
Expand Up @@ -49,5 +49,11 @@ <h1>Remote</h1>
<script>
new Chartkick.LineChart("chart-5", "remote.json");
</script>

<h1>One Series</h1>
<div id="chart-6" style="height: 300px;"></div>
<script>
new Chartkick.LineChart("chart-6", [{name: "Series A", data: {"2013-02-10 00:00:00 -0800":11,"2013-02-11 00:00:00 -0800":6,"2013-02-12 00:00:00 -0800":3,"2013-02-13 00:00:00 -0800":2,"2013-02-14 00:00:00 -0800":5,"2013-02-15 00:00:00 -0800":3,"2013-02-16 00:00:00 -0800":8,"2013-02-17 00:00:00 -0800":6,"2013-02-18 00:00:00 -0800":6,"2013-02-19 00:00:00 -0800":12,"2013-02-20 00:00:00 -0800":5,"2013-02-21 00:00:00 -0800":5,"2013-02-22 00:00:00 -0800":3,"2013-02-23 00:00:00 -0800":1,"2013-02-24 00:00:00 -0800":10,"2013-02-25 00:00:00 -0800":1,"2013-02-26 00:00:00 -0800":3,"2013-02-27 00:00:00 -0800":2,"2013-02-28 00:00:00 -0800":3,"2013-03-01 00:00:00 -0800":2,"2013-03-02 00:00:00 -0800":8}}]);
</script>
</body>
</html>

0 comments on commit f5442e4

Please sign in to comment.