Code Monkey home page Code Monkey logo

chart.js.legend's Introduction

DEPRECATED Please use the official Chart.js legend instead.

Chart.js.legend

An implementation of legend widget for Chart.js.

Simply legend(parent, data). See demo.js for a full example.

If you need to support older browsers (IE8 and such), use src/legend.legacy.js. Otherwise you can stick with src/legend.js.

Quick example

<div id="placeholder"></div>
var data = ...;  // refers to the charts data
legend(document.getElementById('placeholder'), data);

Contributors

  • Laurent Goussard - Simplified markup, support for pie etc.
  • yousifucv - Make CSS border work properly
  • KuenzelIT - Update to support new Chart.js syntax and add example
  • Antonin Bourguignon - Make it easier to customize legend color samples
  • Xu Ding - IE8 and lower compatible version (legend.legacy.js)
  • Alex Belloni - If a chart has tooltips, show legends there
  • TLevasseur - Allow legends to be templated. Handy for formatting.

License

MIT.

chart.js.legend's People

Contributors

alexbelloni avatar bebraw avatar loranger avatar qn7o avatar tlevasseur avatar yousifalyousifi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

chart.js.legend's Issues

Not working in IE8 (and lower) browsers

I add some modifications in legend.js so that it could work in both webkit and IE8 browsers.

function legend(parent, orig_data) {
    parent.className = 'legend';
    var data = orig_data.hasOwnProperty('datasets') ? orig_data.datasets : orig_data;

    while(parent.hasChildNodes()) {
        parent.removeChild(parent.lastChild);
    }

    for (var i in data) {
        d = data[i];
        var title = document.createElement('span');
        title.className = 'title';
        parent.appendChild(title);

        var colorSample = document.createElement('div');
        colorSample.className = 'color-sample';
        colorSample.style.backgroundColor = colorToHex(d.hasOwnProperty('strokeColor') ? d.strokeColor : d.color);
        colorSample.style.borderColor = colorToHex(d.hasOwnProperty('fillColor') ? d.fillColor : d.color);
        title.appendChild(colorSample);

        var text = document.createTextNode(d.label);
        title.appendChild(text);
    }
}

function componentToHex(c) {
    var hex = Number(c).toString(16);
    return hex.length == 1 ? "0" + hex : hex;
}

function rgbToHex(r, g, b) {
    return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}

function colorToHex(color) {
    var matchElements = color.match(/^rgba\((\d+),(\d+),(\d+)/);
    if (matchElements)
        return rgbToHex(matchElements[1], matchElements[2], matchElements[3]);
    else
        return color;
}

How to use for large json with multiple property and multiple propertyValues

[
{
"machineId": "1A",
"serialNo": "123",
"city": "",
"Mode": "ON"
},
{
"machineId": "2B",
"serialNo": "123",
"city": "",
"Mode": "OFF"
},
{
"machineId": "3A",
"serialNo": "123",
"city": "NewCity",
"Mode": "OFF"
},
{
"machineId": "1A",
"serialNo": "123",
"city": "",
"Mode": "OFF"
},
{
"machineId": "1A",
"serialNo": "123",
"city": "",
"Mode": "Default"
}
]

in above json, lets say i want to filter, machineId having value "IA", and Mode having values "ON and OFF"

I tried something like

property: ["machineId", "Mode"],
value: ["1A,ON,OFF"],

it did not work, Please suggest how to use the plugin in this case

Responsive?

I see the newer version of chart.js makes charts responsive - any idea how to implement that with a legend?

This is achieved via

window.onload = function(){
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx).Line(lineChartData, {
responsive: true
});
}

but this isn't referenced in the included demo.js file

Not working with charrtJs Doughnut chart

var doughnutData = [
{
value: 300,
color:"#F7464A",
highlight: "#FF5A5E",
label: "Critical"
},

    {
      value: 100,
      color: "#FDB45C",
      highlight: "#FFC870",
      label: "Warning"
    }


  ];

$( document ).ready(function() {
var ctx = document.getElementById("chart-area").getContext("2d");

    var myDoughnut1 = new Chart(ctx).Doughnut(doughnutData, {responsive : true});

    legend(document.getElementById("legend"), doughnutData1,myDoughnut1);

});

the legend is not working for me

Data values

Good addition to Chart - I couldn't get their legend working. Can I get the data values (actual not percentage) in the legend beside the data labels?

Complete example

Would like to request a complete pie chart and legend example

Can't call legend() method more than once on the same element

For the following markup:

<canvas id="chart" height="250" width="350"></canvas>
<div id="legend"></div>

I am able to running the following code to generate a chart and corresponding legend without any issues the first time through.

var ctx = $('#chart').get(0).getContext('2d');
new Chart(ctx).Line(data);
legend($('#legend').get(0), data);

The issue occurs whenever I want to run this code a second time to update the existing chart and legend with new data. The Chart.js canvas element updates without a problem. The legend, however, continues to append titles to the div element. I am currently working around this issue by manually clearing the content of the legend element before calling the "legend()" method a second time, but it seems as though the plugin should be handling this situation.

Thanks in advance. This plugin has been very useful.

Colors won't show on Legend

this is my code

<?php
  include('dbconn.php');
  include('get-chart-data.php');
?>
<script src="js/vendor/Chart.js"></script>
<script src="js/legend.js"></script>
<div>
  <canvas id="pieChartMonthlyIncome" width="400" height="400" class="text-center"></canvas>
    <div id="pieChartMonthlyIncomeLegend"></div>
      <script type="text/javascript">
        var data1 = [
          {
              value: <?php echo $bonus_sum; ?>,
              color:"#F38630",
              label: 'Bonus'
          },
          {
              value: <?php echo $salary_sum; ?>,
              color : "#E0E4CC",
              label: 'Salary'
          },
          {
              value: <?php echo $pm_sum; ?>,
              color : "#69D2E7",
              label: 'Pocket Money'
          },
          {
              value: <?php echo $refund_sum; ?>,
              color : "#62D2E7",
              label: 'Refund'
          },
          {
              value: <?php echo $i_others_sum; ?>,
              color : "#61D2E7",
              label: 'Others'
          }
      ];

      var ctx1 = document.getElementById("pieChartMonthlyIncome").getContext("2d");
      var pieChart1 = new Chart(ctx1).Pie(data1);

      legend(document.getElementById("pieChartMonthlyIncomeLegend"), data1, pieChart1);=
  </script>
</div>

the pie graph appears fine, but the legend just appears as a normal list. i decided to put the js internally because i can't seem to get around getting the values from php when i'm using the demo.js

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.