dygraphs

repository·master·Indexed 25 days ago

https://github.com/danvk/dygraphs

A fast, flexible open source JavaScript charting library used to produce interactive, zoomable charts of time series data. It supports features such as error bands, pan/zoom, and adjustable averaging periods, and is designed to work without an external server or Flash. Version 2.2.3-alpha.0.

Tokens
2.8K
Snippets
8
Records
17
Agent score
85%

What's inside dygraphs

  1. Install dygraphs via npm

    master

    To use dygraphs in your project with a module bundler (like webpack or browserify), install it using npm.

    Note: Do not install directly from the git repository, as npm may fail to build the source code. Always use the tarball install from the NPM registry.

    npm install dygraphs
  2. Build dygraphs and run tests

    master

    Use the following commands to build the project and execute the test suites.

    Building

    To build the project (using build-jsonly):

    npm run clean  # if necessary
    npm run build-jsonly

    Running Tests

    You can run tests against the unminified bundle or the minified files:

    • Unminified bundle: npm run test
    • Minified files (.min.{css,js}): npm run test-min

    To run a specific test from the command line using a grep pattern, use the -- separator:

    npm run test -- --grep <pattern>
    npm run clean  # if necessary
    npm run build-jsonly
    
    npm run test       # on the unminified bundle
    npm run test-min   # on the .min.{css,js}
    
    npm run test -- --grep highlight-series-background
  3. Follow dygraphs coding style and contribution guidelines

    master

    When contributing to dygraphs, adhere to the following standards to ensure a smooth review process:

    JavaScript Style

    • Follow the Google JS style guide.
    • No tabs: Use two spaces for indentation.
    • Naming: Use camelCase for variable and function names.
    • Line length: Limit lines to 80 characters.

    Feature and Bug Requirements

    • New Features: Add a test in the tests/ directory or create a new gallery entry.
    • New Options: Document any new options in dygraph-options-reference.js to avoid warnings.
    • Bug Fixes/New Features: Always add a unit test in the auto_tests directory (either by adding to an existing test in auto_tests/tests or creating a new one).
  4. Set up the dygraphs development environment

    master

    To prepare the environment for building and testing dygraphs, you need to install system dependencies and npm packages.

    System Dependencies

    For building, install the following via apt-get:

    • jq
    • mksh
    • pax
    • python3

    For a full build including documentation, you also need:

    • ed
    • node-jsdoc2
    • libjs-bootstrap
    • libjs-jquery
    • libjs-jquery-ui

    NPM Dependencies

    Install the project's development dependencies using:

    npm install
    # for building
    apt-get install jq mksh pax python3
    # npm dev dependencies
    npm install
  5. Create a minimal dygraphs chart in HTML

    master

    To use dygraphs directly in an HTML file without a module loader, include dygraph.css and dygraph.js. Use Dygraph.onDOMready() to ensure the DOM is loaded before initializing a new Dygraph instance. The constructor accepts a container element, a data string (CSV format), and an options object.

    <html
    <head>
    <link rel="stylesheet" type="text/css" href="dygraph.css" />
    <script type="text/javascript" src="dygraph.js"></script>
    </head>
    <body
    <div id="graphdiv"></div
    <script type="text/javascript"><!--//--><![CDATA[//><!--
      Dygraph.onDOMready(function onDOMready() {  // or jQuery $() etc.
        g = new Dygraph(
            document.getElementById("graphdiv"),  // containing div
            "Date,Temperature\n" +                // the data series
            "2008-05-07,75\n" +
            "2008-05-08,70\n" +
            "2008-05-09,80\n",
            { }
          );
      });
    //--><!]]></script>
    </body>
    </html>
  6. Iterate on code and unit tests

    master

    To develop features or fix bugs with live feedback, use the watch command and browser-based runners.

    Live Code Iteration

    1. Run the watch command to install envify and monitor changes:
      npm run watch
    2. Open `tests/demo.html` (or another demo file) in your browser to see changes.
    
    ### Unit Test Iteration
    1. Run the `watch` command.
    2. Open `auto_tests/runner.html` in your browser.
    3. Use the Mocha UI to run specific tests or suites, or use `it.only` in your code to isolate a single test.
    
  7. Use the Smooth Plotter extra

    master
    Dygraphs supports a smooth plotting effect using cubic splines (bezier curves) to smooth out lines between data points. This is currently implemented as a separate plotter located in the /extras directory rather than being part of the core library. This plotter uses an algorithm that determines left and right control points for each data point to ensure curves connect without kinks, while capping y-values to prevent false maxima.
  8. Understand the dygraphs plugin architecture

    master
    A single dygraph is composed of multiple plugins, each responsible for a specific part of the chart (e.g., the plot area, axes, legend, or labels). This modular architecture allows for better API separation and extensibility.
  9. Implement a custom dygraphs plugin

    master

    To create a plugin, implement a constructor and the lifecycle methods activate and destroy.

    Lifecycle Methods

    • Constructor: Invoked once per chart. Use this for non-chart-specific initialization. You cannot access the dygraph object here.
    • activate(dygraph, registerer): Called once the dygraph is ready. This is the correct place for:
      • Reading plugin options.
      • DOM manipulation (e.g., appending elements to dygraph.graphDiv).
      • Registering event listeners via the registerer object.
    • destroy(): Used for cleanup (implementation details depend on your specific plugin requirements).
    • toString(): Recommended for debugging purposes to identify the plugin.
  10. Best practices for HTML in gallery entries

    master

    When writing HTML for gallery entries, follow these tips to simplify development:

    • Quote Management: Convert double-quotes in your HTML to single quotes to avoid conflicts with JavaScript string delimiters.
    • Styling: Use the element id in conjunction with specialized CSS for targeting.
    • Large HTML payloads: For entries containing significant amounts of HTML, refer to the independent-series entry for specific implementation tips.
  11. Handle callbacks in HTML widgets for gallery entries

    master
    Functions defined within the run scope of a gallery entry are locally scoped and cannot be accessed by inline HTML event attributes (e.g., <button onclick="func">). To make a function accessible to HTML widgets, you must attach it to the global scope (window).
  12. Define a cleanup function for gallery entries

    master
    When creating entries for the gallery, you can define a cleanup function. This function acts as the inverse of the setup function and is used to perform teardown tasks, such as stopping timers or removing event listeners, to prevent memory leaks or unexpected behavior when the entry is removed.