BBjChart Example

Copy

BBjChart Example

use java.awt.BasicStroke
use java.text.SimpleDateFormat
use org.jfree.data.xy.XYDataset
use org.jfree.data.xy.XYSeries
use org.jfree.chart.axis.DateTickUnit
use org.jfree.chart.JFreeChart
use org.jfree.chart.ChartFactory
use org.jfree.chart.ChartPanel
use org.jfree.chart.plot.XYPlot
use org.jfree.data.xy.XYSeriesCollection
use org.jfree.data.time.TimeSeries
use org.jfree.data.time.TimeSeriesCollection
use org.jfree.data.time.Day
use org.jfree.chart.renderer.xy.XYLineAndShapeRenderer

rem =====
rem Setup
rem =====

sysgui = unt
open (sysgui)"X0"
web = info(3,6)="5" or info(3,6)="6"
x=50,y=50,w=1050,h=600; if web then x=0,y=0
sysgui! = bbjapi().getSysGui()
window! = sysgui!.addWindow(x,y,w,h,"BBjChart Samples",$00090003$)
window!.setCallback(window!.ON_CLOSE,"eoj")

showLegend = 1
is3d = 1

showLegend = 1; rem msgbox("Show Legend?",4+32)=6
is3d = 1; rem msgbox("3d Pie Chart?",4+32)=6

rem ===========
rem BBjPieChart
rem ===========

pieChart! = window!.addPieChart(101,10,10,500,270,showLegend,is3d)
pieChart!.setBackColor(window!.getBackColor())
pieChart!.setToolTipText("BBjPieChart")
pieChart!.setTitle("Receipts by Geographic Area")
pieChart!.setSliceValue("Western Europe",43.2)
pieChart!.setSliceValue("Mexico",10)
pieChart!.setSliceValue("Pacific Rim",17.5)
pieChart!.setSliceValue("Eastern Europe",15.2)
pieChart!.setSliceValue("U.S",32.5)
pieChart!.setSliceValue("Other",6)

pieChart!.setCallback(sysgui!.ON_GAINED_FOCUS,"event")
pieChart!.setCallback(sysgui!.ON_LOST_FOCUS,"event")
pieChart!.setCallback(sysgui!.ON_MOUSE_ENTER,"event")
pieChart!.setCallback(sysgui!.ON_MOUSE_EXIT,"event")
pieChart!.setCallback(sysgui!.ON_POPUP_REQUEST,"event")
pieChart!.setCallback(sysgui!.ON_RIGHT_MOUSE_DOWN,"event")

flags$ = iff(showLegend,$0004$,$0000$)
pieChartLegend! = window!.addCheckBox(102,10,280,500,20,"Pie Chart Legend",flags$)
pieChartLegend!.setCallback(pieChartLegend!.ON_CHECK_ON,"pieChartLegendOn")
pieChartLegend!.setCallback(pieChartLegend!.ON_CHECK_OFF,"pieChartLegendOff")

print "BBjPieChart::is3D ",pieChart!.is3D()
print "BBjPieChart::isLegendShown ",pieChart!.isLegendShown()

rem ===========
rem BBjBarChart
rem ===========

barChart! = window!.addBarChart(103,10,300,500,270,"Fruit","Value",3,5,showLegend,is3d,0)
barChart!.setBackColor(window!.getBackColor())
barChart!.setToolTipText("BBjBarChart")
barChart!.setTitle("Travel Expense by Region")
barChart!.setSeriesName(0,"Europe")
barChart!.setSeriesName(1,"U.S.")
barChart!.setSeriesName(1,"U.S.A.")
barChart!.setSeriesName(2,"Other")
barChart!.setCategoryName(0,"2018")
barChart!.setCategoryName(1,"2019")
barChart!.setCategoryName(2,"2020")
barChart!.setCategoryName(3,"2021")
barChart!.setCategoryName(4,"2022 (est)")
barChart!.setBarValue(0,0,1)
barChart!.setBarValue(0,1,4)
barChart!.setBarValue(0,2,3)
barChart!.setBarValue(0,3,5)
barChart!.setBarValue(0,4,5)
barChart!.setBarValue(1,0,5)
barChart!.setBarValue(1,1,7)
barChart!.setBarValue(1,2,6)
barChart!.setBarValue(1,3,8)
barChart!.setBarValue(1,4,4)
barChart!.setBarValue(2,0,4)
barChart!.setBarValue(2,1,3)
barChart!.setBarValue(2,2,2)
barChart!.setBarValue(2,3,3)
barChart!.setBarValue(2,4,6)

barChart!.setCallback(sysgui!.ON_GAINED_FOCUS,"event")
barChart!.setCallback(sysgui!.ON_LOST_FOCUS,"event")
barChart!.setCallback(sysgui!.ON_MOUSE_ENTER,"event")
barChart!.setCallback(sysgui!.ON_MOUSE_EXIT,"event")
barChart!.setCallback(sysgui!.ON_POPUP_REQUEST,"event")
barChart!.setCallback(sysgui!.ON_RIGHT_MOUSE_DOWN,"event")

flags$ = iff(showLegend,$0004$,$0000$)
barChartLegend! = window!.addCheckBox(104,10,570,500,20,"Bar Chart Legend",flags$)
barChartLegend!.setCallback(barChartLegend!.ON_CHECK_ON,"barChartLegendOn")
barChartLegend!.setCallback(barChartLegend!.ON_CHECK_OFF,"barChartLegendOff")

print "BBjBarChart::is3D ",barChart!.is3D()
print "BBjBarChart::isLegendShown ",barChart!.isLegendShown()

rem ============
rem BBjLineChart
rem ============

if 0 then
rem if msgbox("Scrolling Line Chart?",4+32,"Scrollbars") = 6 then
   rem ' this puts it on a scrollable child window
   scrollingLineChart! = window!.addChildWindow(105,520,10,1000,270,"",$000100804$,1)
   scrollingLineChart!.setTrack(0)
   scrollingLineChart!.setSize(500,270)
   lineChart! = scrollingLineChart!.addLineChart(105,0,0,1000,270,"x","y",2,showLegend)
else
   rem ' this puts it directly on the main window
   lineChart! = window!.addLineChart(105,520,10,500,270,"x","y",2,showLegend)
endif

lineChart!.setBackColor(window!.getBackColor())
lineChart!.setToolTipText("BBjLineChart")
lineChart!.setTitle("Noise levels")
lineChart!.setSeriesName(0,"High frequency")
lineChart!.setSeriesName(1,"Low frequency")
for i = 1 to 200
  x = i/10 - 10
  y = Math.cos(x)
  z = Math.sin(x) * 2
  lineChart!.setXYValue(0,x,y)
  lineChart!.setXYValue(1,x,z)
next i

lineChart!.setCallback(sysgui!.ON_GAINED_FOCUS,"event")
lineChart!.setCallback(sysgui!.ON_LOST_FOCUS,"event")
lineChart!.setCallback(sysgui!.ON_MOUSE_ENTER,"event")
lineChart!.setCallback(sysgui!.ON_MOUSE_EXIT,"event")
lineChart!.setCallback(sysgui!.ON_POPUP_REQUEST,"event")
lineChart!.setCallback(sysgui!.ON_RIGHT_MOUSE_DOWN,"event")

flags$ = iff(showLegend,$0004$,$0000$)
lineChartLegend! = window!.addCheckBox(106,520,280,500,20,"Line Chart Legend",flags$)
lineChartLegend!.setCallback(lineChartLegend!.ON_CHECK_ON,"lineChartLegendOn")
lineChartLegend!.setCallback(lineChartLegend!.ON_CHECK_OFF,"lineChartLegendOff")

print "BBjLineChart::isLegendShown ",lineChart!.isLegendShown()

rem ===============
rem BBjGenericChart
rem ===============

if info(3,6)="5" then goto skip; rem ' BUI
if info(3,6)="6" then goto skip; rem ' DWC

series! = new TimeSeries@("Random Data")
current! = new org.jfree.data.time.Day@(1,1,2021)
value = 62
for i = 0 to 360
  delta = i/60
  value = value + Math.random() - .5
  series!.add(current!,new Double(value + delta))
  current! = current!.next()
next i
dataset! = new TimeSeriesCollection@(series!)

chart! = ChartFactory@.createTimeSeriesChart("2021 Values","Date","Value",dataset!,0,0,0)
color! = window!.getBackColor()
paint! = new java.awt.Color@(color!.getRed(),color!.getGreen(),color!.getBlue())
chart!.setBackgroundPaint(paint!)

list! = bbjapi().makeVector()
list!.add(new Float(10).floatValue())
list!.add(new Float(1).floatValue())
CAP_ROUND = BasicStroke.CAP_ROUND
JOIN_ROUND = BasicStroke.JOIN_ROUND
stroke! = new BasicStroke@(1,CAP_ROUND,JOIN_ROUND,5,list!,0)
renderer! = new XYLineAndShapeRenderer@()
renderer!.setBaseShapesVisible(0)
renderer!.setSeriesStroke(0,stroke!)
plot! = chart!.getPlot()
plot!.setRenderer(renderer!)

format! = new SimpleDateFormat@("MMM-yyyy")
tickUnit! = new DateTickUnit@(DateTickUnit.MONTH,1,format!)
axis! = plot!.getDomainAxis()
axis!.setTickUnit(tickUnit!)
axis!.setVerticalTickLabels(1)

genericChart! = window!.addGenericChart(107,520,300,500,270)
genericChart!.setToolTipText("BBjGenericChart")
genericChart!.setClientChart(chart!)
genericChart!.getClientChartPanel().setMouseZoomable(1,0)

genericChart!.setCallback(sysgui!.ON_GAINED_FOCUS,"event")
genericChart!.setCallback(sysgui!.ON_LOST_FOCUS,"event")
genericChart!.setCallback(sysgui!.ON_MOUSE_ENTER,"event")
genericChart!.setCallback(sysgui!.ON_MOUSE_EXIT,"event")
genericChart!.setCallback(sysgui!.ON_POPUP_REQUEST,"event")
genericChart!.setCallback(sysgui!.ON_RIGHT_MOUSE_DOWN,"event")

skip:

process_events

eoj:
  release

event:
  event! = sysgui!.getLastEvent()
  control! = event!.getControl()
  print control!," ",event!
return

pieChartLegendOn:
  pieChart!.setLegendShown(1,err=*next); rem ' BBj 19.10+
  print "BBjPieChart::isLegendShown ",pieChart!.isLegendShown()
return

pieChartLegendOff:
  pieChart!.setLegendShown(0,err=*next); rem ' BBj 19.10+
  print "BBjPieChart::isLegendShown ",pieChart!.isLegendShown()
return

barChartLegendOn:
  barChart!.setLegendShown(1,err=*next); rem ' BBj 19.10+
  print "BBjBarChart::isLegendShown ",barChart!.isLegendShown()
return

barChartLegendOff:
  barChart!.setLegendShown(0,err=*next); rem ' BBj 19.10+
  print "BBjBarChart::isLegendShown ",barChart!.isLegendShown()
return

lineChartLegendOn:
  lineChart!.setLegendShown(1,err=*next); rem ' BBj 19.10+
  print "BBjLineChart::isLegendShown ",lineChart!.isLegendShown()
return

lineChartLegendOff:
  lineChart!.setLegendShown(0,err=*next); rem ' BBj 19.10+
  print "BBjLineChart::isLegendShown ",lineChart!.isLegendShown()
return