Giter VIP home page Giter VIP logo

bridle's Introduction

Bridle

version 0.3.6

line chart example

A d3 reusable chart library. Bridle includes a line chart, bar chart (with stacked or grouped modes), a stacked area chart and a sortable html table generator.

Installation

Ideally you want to use bower and just type:

bower install bridle

Then you can require it with require.js as you would normally do with any other AMD package

Dependencies

Bridle depends on d3.js and jquery. Make sure they are loaded before you load bridle.

CSS

Bridle comes with a minimal set of css classes to format the tooltips, and do some transitions. Load bridle.css on the head of your document to use.

Usage

While the documentation grows, I recommend becoming familiar with d3 reusable chart idea. Then read through the code of one or two generators to get a better idea of the architecture of the library.

Chart generators

Charts are created using any of the available chart generators (see /src/). All of Bridle's generators are functions that will return a generator object (we capitalise them to highlight that fact):

var barChartGenerator = Bridle.BarChart()

That will create a new chart generator. You can now change it to generate the things you want:

barChartGenerator.title('Chart Demo Party')
  .height(100)
  .width(300)

Each generator has the same basic api:

  • .width(),
  • .height()
  • .margin()
  • .title()

All these methods are both getters and setters. They act as getters if you call them without arguments:

console.log(barChartGenerator.title())
"Chart Title"  //the default value

and setters if you pass an argument:

barChart.generator.title('Chart Demo Afterparty')

Accessing the data

You might have to tell bridle how to access your data. Each chart generator needs to know what is the X and Y values that you're plotting. Some generators might need to know more than that (i.e. the dual chart graph needs to know what you're plotting on the right, and what on the left).

By default, bridle looks for an object like this:

{
  "type": "apples",
  "values": [
    { "x": '2012-01-01', "y":  100*Math.random()},
    { "x": '2012-01-02', "y":  100*Math.random()},
    { "x": '2012-01-03', "y":  100*Math.random()}
  ]
}

But if your data doesn't look like this, you can always tell your bar chart generators where to find the data:

{
  "kind": "pears",
  "measures": [
    { "collectionDate": '2012-01-01', "taste":  100*Math.random()},
    { "collectionDate": '2012-01-02', "taste":  100*Math.random()},
    { "collectionDate": '2012-01-03', "taste":  100*Math.random()}
  ]
}
barChart
  // where to get the series name from
  .nameValue(function(d) {
      return d.kind;
  })
  // where to get the values from 
  .values(function(d) {
    return d.measures
  })
  // where to get it's x value from
  .xValue(function(d) { 
    return d.collectionDate;
  })
  // and where to get it's y value:
  .yValue(function(d) {
    return d.taste;
  });

Drawing your chart

Once you create your chart generator, select an element from your document and attach data to it. then call the chart generator:

d3.select('#elementid')
  .datum(myData)
  .call(barChart)

You could use this on your own draw or render method.

See the Examples for more usage examples.

var lineData = [
  {
    "type": "apples",
    "values": [
      { "z": '2012-01-01', "v":  100*Math.random()},
      { "z": '2012-01-02', "v":  100*Math.random()},
      { "z": '2012-01-03', "v":  100*Math.random()}
    ]
  },
  {  
    "type": "oranges",
    "values": [
      { "z": '2012-01-01', "v":  100*Math.random()},
      { "z": '2012-01-02', "v":  100*Math.random()},
      { "z": '2012-01-03', "v":  100*Math.random()}
    ]
  },
  {
    "type": "pears",
    "values": [
      { "z": '2012-01-01', "v":  100*Math.random()},
      { "z": '2012-01-02', "v":  100*Math.random()},
      { "z": '2012-01-03', "v":  100*Math.random()}
    ]
  },
  {  
    "type": "kiwi",
    "values": [
      { "z": '2012-01-01', "v":  100*Math.random()},
      { "z": '2012-01-02', "v":  100*Math.random()},
      { "z": '2012-01-03', "v":  100*Math.random()}
    ]
  }  
];

var line = Bridle.LineChart()
  .duration(1000)
  .width(800)
  .title("Apples or Oranges?")
  .yAxisTitle("Label your axes")
  .legend(Bridle.LegendBox().height(100).nameAccessor(function(d) {return d.type}))
  .margin({top:50, bottom:30, left:100, right:200})
  .xValue(function (d) {
    return new Date (d.z)
  })
  .yValue(function (d) {
    return d.v
  })
  .nameValue(function (d) {
    return d.type
  });


d3.select('#line-chart')
  .datum(lineData)
  .call(line);

Development

In order to develop you will need to run npm install on the local directory. This will install the dependencies listed on package.json.

To compile the scss file you'll need sass - just install it by running

$ gem install sass

now you can type grunt build to build the js files and the css version of the library

Adding new charts

The concatenation happens on the gruntfile. If you're adding a new module, or a new visualisation, add it between legendbox.js and footer.js, inside initConfig.concat.dist's task

Running tests

At the moment the test are broken with mocha-phantomjs, you'll have to run them in a browser:

$ cd bridle
$ grunt server:test

that will open a browser (if you're using mac os x) and run the mocha tests on the browser.

License

Copyright 2013 Pearson PLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

bridle's People

Contributors

radiodario avatar

Watchers

 avatar  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.