Skip to content

Commit

Permalink
LCJS v4.2.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
Niilo Keinänen committed Aug 8, 2023
1 parent 493019d commit cf716fb
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 234 deletions.
51 changes: 14 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,24 @@ The demo can be used as an example or a seed project. Local execution requires t

_Also known as Bar Graph, Column Chart or Column Graph_

Similarly to **_Column Chart or Vertical Bar Chart_**, this example shows the creation of a column chart made on user side by utilizing RectangleSeries, but displays data as horizontal bars. Bar Charts show discrete numerical comparisons across categories, where the value represents the length of a bar.
Example showing the Horizontal variant of LightningChart JS Bar Chart.
Rotating the Bar Chart is as simple as adding one parameter:

The following code below shows the creation of a Horizontal Bar Chart using a pre-defined interface. Moreover, the current interface allows visualizing columns with negative values.

The bar series accepts points in format `{ category: string, value: number }`. Any number of points can be added with a single call.

```javascript
// Create Chart.
const chart = barChart()

// Add bars.
chart.addValues([
{ category: 'A', value: 20 },
{ category: 'B', value: -30 },
])
```ts
const barChart = lc.BarChart({
type: BarChartTypes.Horizontal,
})
```

The actual Bar Chart logic just serves to provide a starting point for users to create their own API based on their needs and preferences.
In this example scenario, the Bar Chart is used to visualize the temperatures of different cities over the world.
Built-in, automatic sorting is utilized to show the bars in order from hottest to coldest.


## API Links

* [XY cartesian chart]
* [Rectangle series]
* [Solid FillStyle]
* [Empty FillStyle]
* [Solid LineStyle]
* [Empty LineStyle]
* [RGBA color factory]
* [cursor modes]
* [Axis]
* [Scroll strategies]
* [UI position origins]
* [Bar chart]
* [Bar chart Value Axis]
* [Bar chart Category Axis]


## Support
Expand All @@ -77,15 +62,7 @@ Direct developer email support can be purchased through a [Support Plan][4] or b
© LightningChart Ltd 2009-2022. All rights reserved.


[XY cartesian chart]: https://lightningchart.com/lightningchart-js-api-documentation/v4.1.0/classes/ChartXY.html
[Rectangle series]: https://lightningchart.com/lightningchart-js-api-documentation/v4.1.0/classes/RectangleSeries.html
[Solid FillStyle]: https://lightningchart.com/lightningchart-js-api-documentation/v4.1.0/classes/SolidFill.html
[Empty FillStyle]: https://lightningchart.com/lightningchart-js-api-documentation/v4.1.0/variables/emptyFill-1.html
[Solid LineStyle]: https://lightningchart.com/lightningchart-js-api-documentation/v4.1.0/classes/SolidLine.html
[Empty LineStyle]: https://lightningchart.com/lightningchart-js-api-documentation/v4.1.0/variables/emptyLine.html
[RGBA color factory]: https://lightningchart.com/lightningchart-js-api-documentation/v4.1.0/functions/ColorRGBA.html
[cursor modes]: https://lightningchart.com/lightningchart-js-api-documentation/v4.1.0/enums/AutoCursorModes.html
[Axis]: https://lightningchart.com/lightningchart-js-api-documentation/v4.1.0/classes/Axis.html
[Scroll strategies]: https://lightningchart.com/lightningchart-js-api-documentation/v4.1.0/variables/AxisScrollStrategies.html
[UI position origins]: https://lightningchart.com/lightningchart-js-api-documentation/v4.1.0/variables/UIOrigins.html
[Bar chart]: https://lightningchart.com/js-charts/api-documentation/v4.2.0/interfaces/BarChart.html
[Bar chart Value Axis]: https://lightningchart.com/js-charts/api-documentation/v4.2.0/classes/BarChartValueAxis.html
[Bar chart Category Axis]: https://lightningchart.com/js-charts/api-documentation/v4.2.0/classes/BarChartCategoryAxis.html

Binary file modified horizontalBars-cyberSpace.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified horizontalBars-darkGold.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified horizontalBars-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified horizontalBars-lightNature.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified horizontalBars-turquoiseHexagon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
228 changes: 31 additions & 197 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,201 +1,35 @@
/*
* LightningChartJS example for rendering a 'Simple horizontal bar chart'.
/**
* Example of a horizontal BarChart with LightningChart JS.
*/
// Import LightningChartJS
const lcjs = require('@arction/lcjs')

// Extract required parts from LightningChartJS.
const {
lightningChart,
SolidFill,
SolidLine,
ColorRGBA,
emptyLine,
emptyFill,
AxisTickStrategies,
AutoCursorModes,
UIOrigins,
UIElementBuilders,
Themes,
} = lcjs
const { lightningChart, BarChartTypes, BarChartSorting, AxisScrollStrategies, Themes } = lcjs

const lc = lightningChart()

// Define an interface for creating horizontal bars.
let barChart
{
barChart = (options) => {
// flat red fill style for positive bars
const flatRedStyle = new SolidFill().setColor(ColorRGBA(242, 67, 79))
// flat blue fill style for negative bars
const flatBlueStyle = new SolidFill().setColor(ColorRGBA(42, 171, 240))

let y = 0
const figureThickness = 10
const figureGap = figureThickness * 0.5
const bars = []

// Create a XY-Chart and add a RectSeries to it for rendering rectangles.
const chart = lc
.ChartXY(options)
.setTitle('Mass memory production increases in 2018')
.setAutoCursorMode(AutoCursorModes.onHover)
// Disable mouse interactions (e.g. zooming and panning) of plotting area
.setMouseInteractions(false)
.setPadding({ right: 20 })

// Bar series represented with rectangles.
const rectangles = chart.addRectangleSeries()

// cursor
//#region
// Show band using Rectangle series.
const band = chart
.addRectangleSeries()
.setMouseInteractions(false)
.setCursorEnabled(false)
.add({ x: 0, y: 0, width: 0, height: 0 })
.setFillStyle(new SolidFill().setColor(ColorRGBA(255, 255, 255, 50)))
.setStrokeStyle(emptyLine)
.setVisible(false)
// Modify AutoCursor.
chart.setAutoCursor((cursor) =>
cursor
.setResultTableAutoTextStyle(true)
.setPointMarkerVisible(false)
.setTickMarkerXVisible(false)
.setTickMarkerYVisible(false)
.setGridStrokeXStyle(emptyLine)
.setGridStrokeYStyle(emptyLine)
.setResultTable((table) => {
table.setOrigin(UIOrigins.CenterBottom)
}),
)
// Change how marker displays its information.
rectangles.setCursorResultTableFormatter((builder, series, figure) => {
// Find cached entry for the figure.
const entry = bars.find((bar) => bar.rect == figure).entry
// Parse result table content from values of 'entry'.
return builder.addRow('Month: ' + entry.category).addRow('Value: ' + String(entry.value))
})
//#endregion

// X-axis of the series
const axisX = chart
.getDefaultAxisX()
.setMouseInteractions(false)
.setInterval({ start: -100, end: 100, stopAxisAfter: false })
.setTitle('%')

// Y-axis of the series
const axisY = chart
.getDefaultAxisY()
.setMouseInteractions(false)
.setScrollStrategy(undefined)
// Disable default ticks.
.setTickStrategy(AxisTickStrategies.Empty)

//Add middle line
const constantLine = axisX.addConstantLine()
constantLine
.setValue(0)
.setMouseInteractions(false)
.setStrokeStyle(new SolidLine({ thickness: 2, fillStyle: new SolidFill({ color: ColorRGBA(125, 125, 125) }) }))

/**
* Add multiple bars.
* @param entries Add multiple bars data.
*/
const addValues = (entries) => {
for (const entry of entries) {
bars.push(add(entry))
}
}
/**
* Add single bar.
* @param entry Add a single bar data.
*/
const addValue = (entry) => {
bars.push(add(entry))
}
/**
* Construct bar to draw.
* @param entry Single bar data.
*/
const add = (entry) => {
// Create rect dimensions.
const rectDimensions = {
x: 0,
y: y - figureThickness,
width: entry.value,
height: figureThickness,
}
// Add rect to the series.
const rect = rectangles.add(rectDimensions)
// Set individual color for the bar.
rect.setFillStyle(entry.value > 0 ? flatRedStyle : flatBlueStyle).setStrokeStyle(emptyLine)

// Show cursor band when mouse is above figure.
rect.onMouseEnter(() => {
const dimensions = rect.getDimensionsPositionAndSize()
// Show band.
band.setDimensions({
x: rect.scale.x.getInnerStart(),
y: dimensions.y - figureGap * 0.5,
width: rect.scale.x.getInnerInterval(),
height: dimensions.height + figureGap,
}).setVisible(true)
})
rect.onMouseLeave(() => {
band.setVisible(false)
})

// Set view manually.
axisY.setInterval({
start: -(figureThickness + figureGap),
end: y + figureGap,
stopAxisAfter: false,
})

// Add custom tick, more like categorical axis.
axisY
.addCustomTick()
.setValue(y - figureGap)
.setGridStrokeLength(0)
.setTextFormatter((_) => entry.category)
.setMarker((marker) => marker.setTextFillStyle(new SolidFill({ color: ColorRGBA(170, 170, 170) })))
y += figureThickness + figureGap
// Return data-structure with both original 'entry' and the rectangle figure that represents it.
return {
entry,
rect,
}
}

// Return public methods of a bar chart interface.
return {
addValue,
addValues,
}
}
}

// Use bar chart interface to construct series.
const chart = barChart({
// theme: Themes.darkGold
})
// Add multiple bars at once.
chart.addValues([
{ category: 'Jan', value: 20 },
{ category: 'Feb', value: -30 },
{ category: 'Mar', value: -100 },
{ category: 'Apr', value: 100 },
{ category: 'May', value: -75 },
{ category: 'Jun', value: 80 },
{ category: 'Jul', value: -100 },
{ category: 'Aug', value: 35 },
{ category: 'Sep', value: -50 },
{ category: 'Oct', value: 100 },
{ category: 'Nov', value: 5 },
{ category: 'Dec', value: 15 },
])
const barChart = lc
.BarChart({
type: BarChartTypes.Horizontal,
// theme: Themes.darkGold
})
.setTitle('Global temperatures')
.setSorting(BarChartSorting.Descending)

barChart.valueAxis.setTitle('Temperature °C').setInterval({ end: 35 }).setScrollStrategy(AxisScrollStrategies.expansion)

const data = [
{ category: 'Helsinki', value: 19.1 },
{ category: 'New York', value: 20.6 },
{ category: 'London', value: 16.6 },
{ category: 'Tokyo', value: 26.5 },
{ category: 'Oslo', value: 16.3 },
{ category: 'Barcelona', value: 23.7 },
{ category: 'Berlin', value: 18.0 },
{ category: 'Chicago', value: 21.4 },
{ category: 'Beijing', value: 22.4 },
{ category: 'Kathmandu', value: 29.8 },
{ category: 'Kyiv', value: 23.4 },
{ category: 'Singapore', value: 32.6 },
{ category: 'Stockholm', value: 17.2 },
{ category: 'Budapest', value: 21.8 },
{ category: 'Tallinn', value: 21.0 },
]
barChart.setData(data)

0 comments on commit cf716fb

Please sign in to comment.