Wrapper/include for ChartDirector (professional charts)
Posted: Wed Aug 29, 2012 11:36 pm
ChartDirector is a powerful chart component for creating professional looking charts for web and windows applications.
Regrettably, there is no built-in support for Purebasic, so I just started to write a wrapper (many thanks to Danilo !) and to translate the samples of the chartdirector
Below, please find the first version; I'll try to enhance it continously (of course, any help is appreciated):
Regrettably, there is no built-in support for Purebasic, so I just started to write a wrapper (many thanks to Danilo !) and to translate the samples of the chartdirector
Below, please find the first version; I'll try to enhance it continously (of course, any help is appreciated):
Code: Select all
; Wrapper for Chartdirector component & Samples
; http://www.advsofteng.com/gallery.html
; Version 5.1
; PB Code by Danilo & Oliver13
; http://www.purebasic.fr/english/viewtopic.php?f=13&t=51086
; based upon the samples in chartdirector manual
; A fully working demo version of the component can be found here: http://www.advsofteng.com/download.html
; You need the following files (included in chart bundle):
; chartdir51.lib (for compile time only)
; chartdir51.dll (must be shipped together with the application; royalty-fee for licensed users of the chart)
; CHartdirector Constants
#CHD_TopLeft=7
#CHD_TopCenter=8
#CHD_TopRight=9
#CHD_Left=4
#CHD_Center=5
#CHD_Right=6
#CHD_BottomLeft=1
#CHD_BottomCenter=2
#CHD_BottomRight=3
#CHD_Top=#CHD_TopCenter
#CHD_Bottom=#CHD_BottomCenter
#CHD_Transparent = $ff000000
#CHD_Palette = $ffff0000
#CHD_BackgroundColor = $ffff0000
#CHD_LineColor = $ffff0001
#CHD_TextColor = $ffff0002
#CHD_DataColor = $ffff0008
#CHD_SameAsMainColor = $ffff0007
Structure DoubleArray
d.d[0]
EndStructure
ImportC "chartdir51.lib"
CXYChart_create(width.l, height.l, bgColor.l=$ffff0000, edgeColor.l=$ff000000, raisedEffect.l=0)
CXYChart_setPlotArea(*ptr, x.l, y.l, width.l, height.l, bgColor.l=$ff000000, altBgColor.l=-1, edgeColor.l=-1, hGridColor.l=$c0c0c0, vGridColor.l=$ff000000)
CXYChart_addBarLayer(*ptr, *Dat, len.l, color.l=-1, *name=0, depth.l=0)
CXYChart_addLineLayer(*ptr, *Dat, len.l, color.l=-1, *name=0, depth.l=0)
CXYChart_xAxis.i(*ptr)
CXYChart_yAxis.i(*ptr)
CAxis_setLabels(*ptr, *labels, len.l)
CAxis_setLabelStep(*ptr,majorTickStep.l,minorTickStep.l = 0, majorTickOffset.l = 0, minorTickOffset = -$7fffffff)
CAxis_setLabelStyle(*ptr,*font.Character, fontSize.d = 8,fontColor.i = #CHD_TextColor, fontAngle.d = 0)
CAxis_setLinearScale(*ptr,lowerLimit.d, upperLimit.d, majorTickInc.d = 0, minorTickInc.d = 0)
CAxis_setLinearScale2(*ptr, lowerLimit.d, upperLimit.d, *labels, len.l);
CBaseChart_makeChart(*ptr, filename.p-ascii)
CBaseChart_makeChart2(*ptr, format.l, *mem, *len)
CBaseChart_setSize(*ptr, width.l, height.l)
CBaseChart_addLegend(*ptr, x.l, y.l, vertical.l, *font.Character, fontHeight.d)
CBaseChart_addTitle2(*ptr, alignment.l, text.p-ascii, *font.Character=0, fontSize.d=12, fontColor.l=0, bgColor.l=#White, edgeColor.l=0)
CBaseChart_addText(*ptr,x.l,y.l, text.p-ascii, *font.Character=0, fontSize.d=12, fontColor.l=0, alignment.l=#CHD_TopLeft, angle.d = 0, vertical.i = #False)
CBaseChart_destroy(*ptr)
XYChart2BaseChart(*ptr)
CRanTable_create(seed.l, noOfCols.l, noOfRows.l)
CRanTable_setCol(*ptr, colNo.l, minValue.d, maxvalue.d)
CRanTable_setCol2(*ptr, colNo.l, startValue.d, minDelta.d, maxDelta.d,lowerLimit.d = -1E+308, upperLimit.d = 1E+308)
CRanTable_getCol(*ptr, i.l, *d.doublearray, *len)
CRanTable_destroy(*ptr)
CTextBox_setAlignment(*ptr, a.i)
CTextBox_setText(*ptr, text.p-ascii)
EndImport
DataSection
ddat:
Data.d 85, 156, 179.5, 211, 123
labels:
Data.i @"Mon", @"Tue", @"Wed", @"Thu", @"Fri"
EndDataSection
Procedure Barcharts_SimpleBarcharts()
chart = CXYChart_create(250, 250)
basechart = XYChart2BaseChart(chart)
CBaseChart_setSize(basechart,400,400)
CXYChart_setPlotArea(chart, 30, 20, 350, 350)
CXYChart_addBarLayer(chart, ?ddat, 5)
axis = CXYChart_xAxis(chart)
CAxis_setLabels(axis,?labels,5)
CBaseChart_makeChart(basechart,"simplebar.png")
CBaseChart_Destroy(basechart)
EndProcedure
Procedure LineCharts_SimpleLineChart()
; // Create a XYChart object of size 250 x 250 pixels
chart = CXYChart_create(250, 250)
basechart = XYChart2BaseChart(chart)
; // Set the plotarea at (30, 20) And of size 200 x 200 pixels
CXYChart_setPlotArea(chart, 30, 20, 200, 200)
; // Add a line chart layer using the given data
CXYChart_addLineLayer(chart, ?LineChart_Data, 24)
; // Set the labels on the x axis.
axis = CXYChart_xAxis(chart)
CAxis_setLabels(axis,?LineChart_Label, 23)
; // Display 1 out of 3 labels on the x-axis.
CAxis_setLabelStep(axis,3)
; // Output the chart
CBaseChart_makeChart(basechart,"simplelinechart.jpg")
; //free up resources
CBaseChart_Destroy(basechart)
DataSection
;The data for the line chart
LineChart_Data:
Data.d 30, 28, 40, 55, 75, 68, 54, 60, 50, 62, 75, 65, 75, 91, 60, 55,53, 35, 50, 66, 56, 48, 52, 65, 62
LineChart_Data_End:
; The labels for the line chart
LineChart_Label:
Data.i @"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10",@"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", @"21", @"22", @"23", @"24"
LineChart_Label_End:
EndDataSection
EndProcedure
Procedure LineCharts_CompactLineChart()
;//
;// We use a random number generator To simulate the Data from 9:30am To 4:30pm
;// With one Data point every 4 minutes. The total number of points during that
;// period is 106. (7 hours x 15 points/hour + 1)
;//
NoOfPoints=106;
; // Assume we have Not reached the End of the day yet, And only 85 points are
; // available. Create a random table object of 1 col x 85 rows, using 9 As seed.
cRantable = cRanTable_create(9, 1, 85);
;// Set the 1st column To start With 1800 And With random delta from -5 To 5.
CRanTable_setCol2(cRantable, 0,1800, -5, 5)
*dat.DoubleArray
CRanTable_getCol(cRantable, 0,@*Dat, @length.l);
;// Now we obtain the Data into arrays, we can start To draw the chart using
;// ChartDirector
;//
;// Create a XYChart object of size 180 x 180 pixels With a blue background
;// (0x9c9cce)
chart = CXYChart_create(180,180,$9c9cce)
basechart = XYChart2BaseChart(chart)
; // Add titles To the top And bottom of the chart using 7.5pt Arial font. The text
;// is white 0xffffff on a deep blue 0x31319C background.
cbasechart_addTitle2(basechart, #CHD_Top,"STAR TECH INDEX 2003-01-28", @"arial.ttf", 7.5,$ffffff, $31319c);
cbasechart_addTitle2(basechart, #CHD_Bottom,"LATEST STI:1809.41 (+14.51)", @"arial.ttf", 7.5,$ffffff, $31319c);
; // Set the plotarea at (31, 21) And of size 145 x 124 pixels, With a pale yellow
; // (0xffffc8) background.
cxychart_setPlotArea(chart,31, 21, 145, 124, $ffffc8);
; // Add custom text at (176, 21) (top right corner of plotarea) using 11pt Times
; // Bold Italic font/Red (0xc09090) color
textbox=CBasechart_addtext(basechart,176, 21, "Chart Demo", @"timesbi.ttf", 11, $0c09090)
CTextBox_setAlignment(textbox,#CHD_TopRight)
; // Use 7.5 pts Arial As the y axis label font
yaxis = CXYChart_yAxis(chart)
CAxis_setLabelstyle(yaxis,@"",7.5)
; // Set the labels on the x axis by spreading the labels evenly between the first
;// Point (index = 0) And the last Point (index = noOfPoints - 1)
xaxis = CXYChart_xAxis(chart)
CAxis_setLinearScale2(xaxis,0,noOfPoints - 1,?CompactLineChart_Labels,15)
Caxis_setLabelStyle(xaxis,@"", 7.5);
; // Add a deep Blue (0x000080) line layer To the chart
CXYChart_addLineLayer(chart, *Dat,length,$000080);
; // Output the chart
CBaseChart_makeChart(basechart,"compactlinechart.jpg")
; //free up resources
CRanTable_destroy(crantable)
CBaseChart_Destroy(basechart)
DataSection
;The labels for the line chart
CompactLineChart_Labels:
Data.i @"-", @"10am", @"-", @" ", @"-", @"12am", @"-", @" ", @"-", @"2pm",@"-", @" ", @"-", @"4pm", @"-"
CompactLineChart_Labels_End:
EndDataSection
;
EndProcedure
;Do the samples
Barcharts_SimpleBarcharts()
LineCharts_SimpleLineChart()
LineCharts_CompactLineChart()