Giter Site home page Giter Site logo

chart.js's Introduction

Chart.js

Simple HTML5 Charts using the canvas element chartjs.org

Changes

This adds a mouseover and mouseout event to the data points in a line graph (events for all graphs may follow shortly). I implemented it because I needed to add tooltips for the data points, but you can use the event to do whatever you like.

To hook into the events, just specify a mouseover and mouseout event handler for your dataset. This works for browser mouse events as well as mobile touch events.

var data = {
  labels : labels,
  datasets : [
    {
      fillColor : "rgba(151,187,205,0.5)",
      strokeColor : "rgba(151,187,205,1)",
      pointColor : "rgba(151,187,205,1)",
      pointStrokeColor : "#fff",
      data : values,
      mouseover: function(data) {
        // data returns details about the point hovered, such as x and y position and index, as
        // well as details about the hover event in data.event
        // You can do whatever you like here, but here is a sample implementation of a tooltip
        var active_value = values[data.point.dataPointIndex];
        var active_date = labels[data.point.dataPointIndex];
        // For details about where this tooltip came from, read below
        tooltip.html(active_date + " - " + active_value).css("position", "absolute").css("left", data.point.x-17).css("top", data.point.y-55).css("display", 'block');
      },
      mouseout: function (data) {
        // Hide the tooltip
        tooltip.html("").css("display", "none");
      }
    }
  ]
};

var ctx = document.getElementById("myCanvasElement").getContext("2d");
var chart = new Chart(ctx).Line(data);

As you can see above, we can easily add an HTML tooltip to the data point by using the x and y position of the point hovered. Above, I offset the tooltip by a few pixels, position it absolutely, and format using plain old CSS/HTML. The absolute position works because I wrap my canvas in a relative div and put the tooltip as a sibling to the canvas:

<div style="position: relative;">
  <canvas id="myCanvasElement" width="400" height="400"></canvas>
  <div id="tooltip"></div>
</div>

Make your tooltips as fancy as you like now! My implementation: Sample Chart.js Tooltip

Documentation

You can find documentation at chartjs.org/docs.

License

Chart.js was taken down on the 19th March. It is now back online for good and IS available under MIT license.

Chart.js is available under the [MIT license] (http://opensource.org/licenses/MIT).

chart.js's People

Contributors

nnnick avatar jhdavids8 avatar robocoder avatar esamattis avatar fizerkhan avatar

Watchers

James Cloos avatar Arnas avatar

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.