Skip to content

andydlindsay/bar_gen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Bar-Gen

The customizable bar chart generator

Table of contents

About

Bar-Gen is a bar chart creation program that converts your data into attractive charts. Simple to use with many customizable features.

NOTE: This library requires jQuery.

Usage

Add bar-gen.js and bar-gen.css to your project folder. Include them and a link to jQuery in your index page.

<head>
  <!-- Load bar-gen CSS -->
  <link rel="stylesheet" href="bar-gen.css">
</head>
<body>
  <!-- Don't forget to load jQuery -->
  <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
  <!-- Load bar-gen script -->
  <script src="bar-gen.js"></script>
</body>

Make sure to load jQuery and the bar-gen files before your own custom css and js files.

Call the bar chart generator using the drawBarChart function:

drawBarChart(data, options, element);
  • data
    • A nested array of values including the labels for the x-axis.
  • options
    • An object containing the various customizable aspects of the bar chart including chart title, font colors, axis colors, etc.
  • element
    • A DOM or jQuery element that the chart will be rendered into.

Example

Javascript

var data = [
  [ 10, "Monday" ],
  [ 14, "Tuesday" ],
  [ 12, "Wednesday" ],
  [ 19, "Thursday" ],
  [ 11, "Friday" ]
];
var options = {
  width: 500,
  height: 300,
  labelColor: "darkMagenta",
  yAxisTicks: [ 0, 5, 10, 15, 20 ],
  chartTitle: "Employees Who Showed Up For Work",
  titleFontSize: 24,
  spacerWidth: 3
};
drawBarChart(data, options, $("#barchart"));

Output

generated barchart

Parameters

The data parameter

The data parameter must be a nested array containing the data for the chart as well as the labels for the x-axis. For example:

var data = [
  [ 10, "Monday" ],
  [ 14, "Tuesday" ],
  [ 12, "Wednesday" ],
  [ 19, "Thursday" ],
  [ 11, "Friday" ]
];

Ensure that the first element in the nested array is the data value and the second is the x-axis label.

The options object

Bar-Gen has quite a few customizable options. Use the options object to pass these values into the function. All of the following customization options except width and height are optional.

Parameter Optional Summary Default
axisBorderWidth Yes The width or thickness (in pixels) of the axes. 2
axisColor Yes The color of the x and y axes. "black"
barColors Yes An array containing colors for the bar elements. If the number of colors in barColors is less than the total number of data points passed in, then the colors will repeat in the order they are stated. [ "slateGrey" ]
chartTitle Yes The title that appears above the rendered chart. "Chart"
fontColor Yes The font color for the values that are displayed on the bar elements. "white"
height No The total height (in pixels) of the element that the bar chart will be rendered into. The chart will not exceed this overall height. The minimum height is 100. N/A
labelColor Yes The color of the axes' labels. "black"
showAnimation Yes Show an animation when the chart is rendered. false
showTooltips Yes A boolean value to display or hide tooltips for the chart. false
spacerWidth Yes The space (in pixels) between each bar element. 2
titleFontColor Yes The color of the title text. "black"
titleFontSize Yes The font size (in pixels) of the title. 24
valuePosition Yes The position within the bar element where the value will be displayed. Choices are top, center, or bottom. "top"
width No The total width (in pixels) of the element that the bar chart will be rendered into. The chart will not exceed this overall width and the width of the individual bar elements will be based on this value. N/A
xAxisHeight Yes The height (in pixels) of the x-axis. 20
yAxisTicks Yes An array showing where the ticks on the y-axis should be located. The y-axis will render to the larger of either the largest value in the yAxisTicks array or the largest value in the dataArray array. Generated based on 0, 20, 40, 60, 80, and 100% of the largest data value
yAxisWidth Yes The width (in pixels) of the y-axis. 40

The element parameter

This is the element that the chart will be rendered into. Bar-Gen accepts an element selected via javascript's document.getElementById("chart") or jQuery's $("#chart").

Known issues or bugs

  • Does not show negative values

Roadmap and upcoming features

  • Generate stacked bar charts
  • Handle negative values
  • Specify non-zero starting point for y-axis
  • Generate horizontal bar charts
  • Generate grouped bar charts
  • Generate horizontal stacked bar charts

External resources used

About

Bar-Gen: A Customizable Bar Chart Generator

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published