0.2.3 • Published 3 years ago

trading-charts v0.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

CandlestickChart

An open-source single-file JavaScript library for drawing candlestick charts.

NPM: https://www.npmjs.com/package/trading-charts

Basic Usage

Include the library:

<script src="CandlestickChart.js"></script>

Define an HTML div element as a parent for the chart. Please note that the position, width, and height style values of the div are overwritten, so you may want to wrap the div in another div for additional styling.

<div id="chart"></div>

Create the candlestick chart with the id of the div as the first argument and a second optional options argument (see below for all options):

var candlestickChart = new CandlestickChart( "chart" , {} );

Add a few candles:

for ( var i = 0 ; i < candles.length ; ++i )
{
    candlestickChart.addCandlestick( candles[i].timestamp , candles[i].open , candles[i].high , candles[i].low , candles[i].close );
}

And draw the chart:

candlestickChart.draw();

Updates

If you are using the chart to display live data that frequently updates you can use the updateCandlestick function. If a candle with the same timestamp already exists in the chart, it will be updated with the new values. If not, a new candle is added automatically.

candlestickChart.updateCandlestick( candlestick.timestamp , candlestick.open , candlestick.high , candlestick.low , candlestick.close );

Example

See the example.html file for a more detailed example.

Options

OptionDescription
overlayModeDefines the behavior of the overlay. Possible values:
'click' - The overlay&crosshair only appear when the mouse or a finger is clicked. While the overlay is visible dragging the mouse or a finger moves the overlay. It can be hidden again with another click. Best for touch controls
'hover' - The overlay&crosshair are always visible and follow the mouse cursor. This best for mouse controls.
'none' - The overlay&crosshair are disabled
disableGridSet to true to disable the grid
disableLegendSet to true to disable the legends
disableCrosshairSet to true to disable the crosshair
disableLowHighPriceDisplaySet to true to disable the low and high price markers in the chart
disableCurrentMarketPriceSet to true to disable the current market price overlay (i.e. the close price of the last candle)
disablePanningAccelerationSet to true to disable panning acceleration. When dragging the chart along the x-axis and letting go with some momentum, it will slowly decay if panning acceleration is enabled.
allowOverPanningWhen enabled you can pan the chart further into the empty space behind the last candle.

General Chart Options

backgroundColorThe background color of the chart. E.g. '#222222'.
fontThe font used in the chart. Default 'sans-serif'.
fontSizeThe font size used in the chart. Default 12.
decimalPlacesThe number of decimal places that all prices are rounded to. If no decimalPlaces option is specified, the chart will choose a sensible default value.
marginLeftThe distance between the left edge of the canvas and the chart area.
marginRightThe distance between the right edge of the canvas and the chart area.
marginTopThe distance between the top edge of the canvas and the chart area.
marginBottomThe distance between the bottom edge of the canvas and the chart area.
watermarkImageA url to a watermark image that is displayed in the center of the chart. You probably want to use a fairly transparent png image.

Candle Options

greenColorThe color for green candles.
redColorThe color for red candles.
greenHoverColorThe color for green candles that are hovered or clicked.
redHoverColorThe color for red candles that are hovered or clicked.
wickWidthThe width of the wicks. Uneven numbers look better. Default 1.
wickGreenColorThe color of the wick for green candles. By default this is the same green color as the candle's green color.
wickRedColorThe color of the wick for red candles. By default this is the same red color as the candle's green color.
candleBorderWidthThe border width of the candles. Default 0. If the candleBorderWidth is greater than 0, the border of the candle is drawn in the wickGreenColor&wickRedColor.

Grid Options

xGridCellsHow many grid cells are on the x-axis. Please note that this is only a rough estimate. The grid will automatically align to nice numbers.
yGridCellsHow many grid cells are on the y-axis. Please note that this is only a rough estimate. The grid will automatically align to nice numbers.
gridLineStyleThe line style of the grid. Can either be 'dashed', 'dotted', or 'solid'.
gridColorThe color of the grid lines.
gridTextColorThe text color of the axis labels.

Overlay Options

overlayBackgroundColorThe background color of the overlay.
overlayTextColorThe text color of the overlay.
overlayShowTimeWhen true the overlay will show the candle time. Default true.
overlayShowDataWhen true the overlay will show the candle data. Default true.
overlayShowChangeWhen true the overlay will show the change and percent change relative to the previous candle. Default true.

Crosshair Options

crosshairColorThe color of the crosshair.
crosshairTextColorThe text color of the crosshair markers on the axes.
crosshairLineStyleThe line style of the crosshair. Can either be 'dashed', 'dotted', or 'solid'.
crosshairLabelBackgroundColorThe background color of the crosshair labels. By default this is just the crosshair color.

Market Price Options

marketPriceLineColorThe color of the current market price line.
marketPriceTextColorThe text color of the current market price marker.
marketPriceOpacityThe opacity of the market price background label. A value between 0 and 1, 0 being completely transparent and 1 complete opaque. Default 1.

Control Options

scrollZoomSensitivityThe zoom sensitivity when using the mouse scroll wheel.
touchZoomSensitivityThe zoom sensitivity when using touch controls and pinch to zoom.
panningAccelerationDampingIf panning acceleration is enabled, this controls how fast the chart slows down. A value between 0 and 1. Default 0.975. Lower values will make the chart stop faster.

Callback Functions

panningAtStartCallback()Gets called when the chart is panned/scrolled along the x-axis and reaches the first candle. See example.html for an example implementation.
panningAtEndCallback()Gets called when the chart is panned/scrolled along the x-axis and reaches the last candle. See example.html for an example implementation.