Charts and drawing
JChart draws bar, line, scatter and pie charts on a JCanvas, and the shapes of JCanvas draw anything else.
JChart makes a chart from lists: it draws the axes, the grid, the labels, the legend and the title. The chart is a JCanvas (see Window builders), so you can draw more on it, save it as a picture, or put it in a window.
load "java.ring"
oChart = new JChart(520,320)
oChart.title("Sales by month").axisTitles("Month","Units")
oChart.bars(["Jan","Feb","Mar"],[ ["2025",[120,150,90]], ["2026",[140,170,130]] ])
oWin = jWindow("Sales",560,400)
oWin.add([oChart.component(),"Center"])
jShow(oWin)
jMainLoop()A series is [name, values]. For one series the list of values is enough: oChart.bars(["A","B","C"],[10,20,30]). The scale of the axis is chosen for you (1, 2 or 5 times a power of ten, about five steps, with the zero when there are bars).
| Method | Chart |
|---|---|
bars(labels, series) | one group of bars for each label, one bar of each series in a group (negative values go under the axis) |
lines(labels, series) | the values of each series joined by a line, with a dot at each value |
scatter(series) | each series is [name, [ [x,y], [x,y], ... ]]; both axes are numbers |
pie(labels, values) | a slice for each label, as large as its value, and a legend with the shares |
title(text), axisTitles(x, y), showValues(true) | the title, the titles of the axes, the value written over each bar and in the legend of a pie |
canvas(), component(), save(file) | the JCanvas under the chart; the Swing control; save the picture |
The methods give the chart back, so oChart.title("A").showValues(true).bars(...) is one statement. The colors of the series are the ten colors of jvChartColor; a chart with more than one series has a legend under the title.
More shapes on JCanvas#
The shapes that a chart is made of are methods of JCanvas too:
| Method | Meaning |
|---|---|
arc(x,y,w,h,start,sweep), fillArc(...) | an arc and a slice of the oval in the rectangle; angles in degrees, 0 at 3 o'clock, positive is counter-clockwise |
roundRect(x,y,w,h,arc), fillRoundRect(...) | a rectangle with round corners |
polygon(points), fillPolygon(points), polyline(points) | points are [ [x1,y1], [x2,y2], ... ]; a polyline is not closed |
textWidth(text), textHeight() | the size of a text in the current font, to center or align it |
Drawing pixel by pixel#
setPixels draws a picture that a formula makes (see Window builders); 16_julia.ring draws a Julia set while the mouse moves over a small Mandelbrot picture, 43,000 pixels in about 120 milliseconds of Ring and 3 of Java.
samples/UsingJava/gui/16_julia.ring
# A Julia set: the same formula as the Mandelbrot set, z = z * z + c, but c is fixed for the whole picture and z starts at the pixel.
# Press and drag the mouse over the small Mandelbrot picture (left) to choose c: the Julia set of that point is drawn on the right, in one setPixels() call.
load "java.ring"
nWidth = 240 # the size of each of the two pictures
nHeight = 180
nMaxSteps = 40
nCr = -0.8 # the point c
nCi = 0.156
lBusy = false
aPalette = []
oWin = jWindow("Julia set",nWidth * 2 + 50,nHeight + 100)
oMap = new JCanvas(nWidth,nHeight) # the Mandelbrot set: the places where c can be chosen
oJulia = new JCanvas(nWidth,nHeight)
oStatus = jLabel(" ")
oWin.add([jBorder(jRow([oMap.component(),oJulia.component()],10),NULL,jPad(oStatus,8),NULL,NULL),"Center"])
oMap.onPress("onChoose")
oMap.onDrag("onChoose")
makePalette()
oMap.setPixels(0,0,nWidth,nHeight,computeMap())
oMap.update()
jShow(oWin)
drawJulia()
jAutoClose()
jMainLoop()
# The mouse over the small picture: the point under it is c (x from -2.2 to 1, y from 1.2 to -1.2)
func onChoose nX, nY
if lBusy or nX < 0 or nY < 0 or nX >= nWidth or nY >= nHeight
return
ok
nCr = -2.2 + nX * 3.2 / nWidth
nCi = 1.2 - nY * 2.4 / nHeight
drawJulia()
func drawJulia
lBusy = true
nStart = java_now()
cPixels = computeJulia()
nComputeTime = java_now() - nStart
nStart = java_now()
oJulia.setPixels(0,0,nWidth,nHeight,cPixels)
oJulia.update()
oStatus.setText("c = " + floor(nCr * 1000) / 1000 + " + " + floor(nCi * 1000) / 1000 + "i: computed in " + floor(nComputeTime) + " ms, drawn in " + floor(java_now() - nStart) + " ms")
lBusy = false
# The colors of the Julia set of c, row after row: z starts at the point of the pixel (x from -1.6 to 1.6, y from 1.2 to -1.2)
func computeJulia
cPixels = ""
cBlack = char(0) + char(0) + char(0)
nStepX = 3.2 / nWidth
nStepY = 2.4 / nHeight
for nRow = 1 to nHeight
nY = 1.2 - (nRow - 0.5) * nStepY
cLine = ""
for nCol = 1 to nWidth
nZr = -1.6 + (nCol - 0.5) * nStepX
nZi = nY
nStep = 0
while nStep < nMaxSteps
nZr2 = nZr * nZr
nZi2 = nZi * nZi
if nZr2 + nZi2 > 4
exit
ok
nZi = 2 * nZr * nZi + nCi
nZr = nZr2 - nZi2 + nCr
nStep++
end
if nStep >= nMaxSteps
cLine += cBlack
else
cLine += aPalette[nStep + 1]
ok
next
cPixels += cLine
next
return cPixels
# The small Mandelbrot picture (the same colors, fewer steps)
func computeMap
cPixels = ""
cBlack = char(0) + char(0) + char(0)
nStepX = 3.2 / nWidth
nStepY = 2.4 / nHeight
for nRow = 1 to nHeight
nY = 1.2 - (nRow - 0.5) * nStepY
cLine = ""
for nCol = 1 to nWidth
nX = -2.2 + (nCol - 0.5) * nStepX
nZr = 0
nZi = 0
nStep = 0
while nStep < nMaxSteps
nZr2 = nZr * nZr
nZi2 = nZi * nZi
if nZr2 + nZi2 > 4
exit
ok
nZi = 2 * nZr * nZi + nY
nZr = nZr2 - nZi2 + nX
nStep++
end
if nStep >= nMaxSteps
cLine += cBlack
else
cLine += aPalette[nStep + 1]
ok
next
cPixels += cLine
next
return cPixels
func makePalette
aPalette = []
for nSteps = 0 to nMaxSteps - 1
nHue = (0.6 + nSteps * 0.035) % 1
nSector = floor(nHue * 6)
nFraction = nHue * 6 - nSector
nP = 38
nQ = floor((1 - 0.85 * nFraction) * 255 + 0.5)
nT = floor((1 - 0.85 * (1 - nFraction)) * 255 + 0.5)
switch nSector % 6
on 0
aPalette + (char(255) + char(nT) + char(nP))
on 1
aPalette + (char(nQ) + char(255) + char(nP))
on 2
aPalette + (char(nP) + char(255) + char(nT))
on 3
aPalette + (char(nP) + char(nQ) + char(255))
on 4
aPalette + (char(nT) + char(nP) + char(255))
other
aPalette + (char(255) + char(nP) + char(nQ))
off
nextChart sample#
17_charts.ring shows the four kinds of chart in a tabbed window.
samples/UsingJava/gui/17_charts.ring
# JChart: a bar chart, a line chart, a scatter plot and a pie, one on each tab. The data are lists; JChart draws the axes, the grid and the legend.
load "java.ring"
aMonths = ["Jan","Feb","Mar","Apr","May","Jun"]
oBars = new JChart(520,320)
oBars.title("Sales by month").axisTitles("Month","Units").showValues(true)
oBars.bars(aMonths,[ ["2025",[120,150,90,180,210,160]], ["2026",[140,170,130,200,190,240]] ])
oLines = new JChart(520,320)
oLines.title("Temperature").axisTitles("Month","Degrees")
oLines.lines(aMonths,[ ["Cairo",[14,16,19,24,28,31]], ["London",[5,5,8,11,15,18]], ["Moscow",[-6,-5,0,8,15,19]] ])
aPoints = []
for n = 1 to 40
aPoints + [n / 4,sin(n / 4) * 3 + n / 5]
next
oScatter = new JChart(520,320)
oScatter.title("A sine wave and a slope")
oScatter.scatter([ ["sin(x) * 3 + x / 5",aPoints] ])
oPie = new JChart(520,320)
oPie.title("Where the time goes").showValues(true)
oPie.pie(["Work","Sleep","Meals","Travel","Free"],[8,7,2,1.5,5.5])
oWin = jWindow("Charts",560,400)
oTabs = new JTabbedPane
oTabs.addTab(["Bars",oBars.component()])
oTabs.addTab(["Lines",oLines.component()])
oTabs.addTab(["Scatter",oScatter.component()])
oTabs.addTab(["Pie",oPie.component()])
oWin.add([oTabs,"Center"])
jShow(oWin)
jAutoClose()
jMainLoop()