I hesitate to post this much code in a thread, but I think all of this would be helpful and give you a good headstart.
Essentially, you have an open table/cursor where the first column is assumed to be the labels and the second column is assumed to be the value. As you'll note, I've kludged in support for a second value column, but haven't gotten around to expanding it and making it more flexible.
An example select statement that could be used to feed the graph:
[tt]select State, count(*) from MyTable group by State[/tt]
The result will be a GIF file. Many of the parameters are optional (such as image size), so its fairly simple to use.
------------
[tt]* -- makes a chart using the currently open table/cursor
* -- the first column should be the X-axis labels,
* -- and the rest of the columns should be the Y-axis values
Parameters cOutFile, cCaption, cChartType, nWidthPixels, nHeightPixels, nValueColumns
set asserts on
if empty(alias())
wait window "No table/alias open in current work area" timeout 200
return .F.
endif
if reccount() = 0
wait window "No records in alias" timeout 200
return .F.
endif
if empty(cChartType)
cChartType = "BAR"
else
cChartType = upper(cChartType)
endif
if empty(nWidthPixels)
nWidthPixels = 500
endif
if empty(nHeightPixels)
nHeightPixels = 300
endif
if empty(nValueColumns)
nValueColumns = 1
endif
* -- put the contents into an array
dimension aLabels(reccount())
dimension aValues(reccount())
go top
for i = 1 to reccount()
aLabels(i) = alltrim(evaluate(field(1)))
aValues(i) = evaluate((field(2)))
skip
endfor
if nValueColumns = 2
dimension aLabels2(reccount())
dimension aValues2(reccount())
go top
for i = 1 to reccount()
aLabels2(i) = alltrim(evaluate(field(1)))
aValues2(i) = evaluate((field(3)))
skip
endfor
endif
if empty(cCaption)
cCaption = "Graph"
endif
if empty(cOutFile)
cOutFile = "chart.gif"
endif
* -- Create a Chart Object
oChart = CreateObject("OWC.Chart"
if vartype(oChart) <> "O"
wait window "Could not create OWC object" timeout 30
return .F.
endif
lLegend = .F.
* -- determine graph type
do case
case cChartType == "COLUMN CLUSTERED"
cGraphType = oChart.Constants.chChartTypeColumnClustered
case cChartType == "COLUMN STACKED"
cGraphType = oChart.Constants.chChartTypeColumnStacked
case cChartType == "AREA"
cGraphType = oChart.Constants.chChartTypeArea
case cChartType == "AREA STACKED"
cGraphType = oChart.Constants.chChartTypeAreaStacked
lLegend = .T.
case cChartType == "PIE"
cGraphType = oChart.Constants.chChartTypePie
lLegend = .T.
case cChartType == "PIE EXPLODED"
cGraphType = oChart.Constants.chChartTypePieExploded
lLegend = .T.
case cChartType == "PIE PERCENT"
cGraphType = oChart.Constants.chChartTypePie
lLegend = .T.
case cChartType == "LINE"
cGraphType = oChart.Constants.chChartTypeLine
case cChartType == "DOUGHNUT"
cGraphType = oChart.Constants.chChartTypeDoughnut
case cChartType == "BUBBLE"
cGraphType = oChart.Constants.chChartBubble
otherwise
cGraphType = oChart.Constants.chChartTypeColumnClustered
endcase
*** oChart.Border.Color = c.chColorNone
oChart.Charts.Add
oChart.Charts(0).Type = cGraphType && oChart.Constants.chChartTypeColumnClustered
oChart.Charts(0).HasTitle = .T.
oChart.Charts(0).Title.Caption = cCaption
oChart.Charts(0).SeriesCollection.Add
oChart.Charts(0).SeriesCollection(0).Caption = field(2)
if "PIE PERCENT" $ upper(cChartType)
oDL = oChart.Charts(0).SeriesCollection(0).DataLabelsCollection.Add
oDL.HasPercentage = .T.
oDL.HasCategoryName = .F.
oDL.HasValue = .F.
endif
oChart.Charts(0).SeriesCollection(0).SetData(oChart.Constants.chDimCategories, oChart.Constants.chDataLiteral, @aLabels)
oChart.Charts(0).SeriesCollection(0).SetData(oChart.Constants.chDimValues, oChart.Constants.chDataLiteral, @aValues)
if nValueColumns = 2
oChart.Charts(0).SeriesCollection.Add
oChart.Charts(0).SeriesCollection(1).Caption = field(3)
if "PIE PERCENT" $ upper(cChartType)
oDL = oChart.Charts(0).SeriesCollection(1).DataLabelsCollection.Add
oDL.HasPercentage = .T.
oDL.HasCategoryName = .F.
oDL.HasValue = .F.
endif
oChart.Charts(0).SeriesCollection(1).SetData(oChart.Constants.chDimCategories, oChart.Constants.chDataLiteral, @aLabels2)
oChart.Charts(0).SeriesCollection(1).SetData(oChart.Constants.chDimValues, oChart.Constants.chDataLiteral, @aValues2)
endif
oChart.Charts(0).HasLegend = lLegend
oChart.ExportPicture(cOutFile, "gif", nWidthPixels, nHeightPixels)
release oChart
[/tt]
Robert Bradley