MoonChart is an SVG charting library written in MoonBit. It produces vector graphics suitable for scientific and statistical plotting — bar charts, line charts, scatter plots, and box plots — with err
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802047560号
MoonChart
MoonChart is an SVG charting library written in MoonBit. It produces vector graphics suitable for scientific and statistical plotting — bar charts, line charts, scatter plots, and box plots — with error bars, multi-series rendering, and extensive style controls. Output is an SVG string that can be saved to a file or rendered in a browser via WASM.
Package:
JunJunTvT/moonchartRepository: https://github.com/JunJunTvT/moonchart License: Apache-2.0Installation
Then import it in your MoonBit source:
Simple Demo
Quick Start
A minimal bar chart with error bars:
Chart Types
Bar Chart
Rectangular bars with optional error bars, grouped or stacked positioning, and bar stroke styling.
Line Chart
Connected points with configurable dash styles, point shapes, and point fill colors.
Scatter Plot
Unconnected points with customizable shape, size, and fill color.
Box Plot
Box-and-whisker plot with Q1/Q3 box, median line, whiskers to the furthest points within 1.5x IQR, and outlier markers.
Mixed Charts
Series of different types can be combined in a single chart:
Feature Matrix
Features below are per-series capabilities — attributes you set on individual
Seriesobjects via constructor modifiers.Builder API Reference
Chart Builder
All methods return a new
Chart(immutable builder pattern).Chart::new().title(text).size(w, h).width(w).height(h).xLabel(text).yLabel(text).xTicks(positions, labels).yRange(min, max).tickFontSize(size).labelFontSize(size).yGrid(show).xGrid(show).barWidthRatio(ratio).barGap(gap).legend(show, position).legendFontSize(size).margin(top, right, bottom, left).background(color).fontFamily(family).series(s).addSeries(ss).toSvg()Result[String, String]).save(filename)Result[Unit, String]).toOption()ChartOption(escape hatch)Series Constructors
Series::bar(name, data)Series::line(name, data)Series::scatter(name, data)Series::boxplot(name, groups)Series Modifiers
.withColor(color).withLineWidth(w).withPointSize(s).withPointShape(shape).withLineStyle(style).withPointFill(color).withBarStroke(color, w).withErrorColor(color)Noneto inherit series color).withErrorLineWidth(w).withErrorCapWidth(w).withShowOutliers(show)Data Types
Enums
PointShape:
Circle | Square | Triangle | Diamond | CrossLineStyle:
Solid | Dashed | Dotted | DashDotPosition:
TopLeft | TopCenter | TopRight | BottomLeft | BottomCenter | BottomRight | Left | RightColor
The default palette is a 6-color colorblind-friendly sequence: blue, orange, green, red, purple, brown. Override per-series with
.withColor().Axis Configuration
Static API
WASM Usage
MoonChart can be compiled to WASM-GC for browser use. A dedicated WASM module is provided at
wasm/wasm_api.mbtthat exposes a single function:It accepts a JSON configuration string and returns an SVG string. The WASM package is configured in
wasm/moon.pkg.jsonwithrender_chartas the exported function.Build for WASM:
This enables rendering MoonChart graphics directly in the browser from JavaScript, with all chart types, error bars, and styling options available via a JSON-based API.
License
Apache 2.0