Thursday, November 5, 2009

Draw 3D pie diagram in Excel from magik


This piece of code will help to draw a 3d pie diagram in excel sheet from magik..


_block

e << OLE_Client.Create_Object("Excel.Application")
w << e.Workbooks()

w.add()
s << e.Sheets("Sheet1")
s.Name << "bhimu"
_for i _over range (1,10)
_loop
Cell << S.Range ("A"+i.print_string)
Cell.Value << i
Cell.Release_Object ()
Cell << S.Range ("B"+i.print_string)
Cell.Value << "=A"+i.print_string+"*"+"A"+i.print_string
Cell.Release_Object ()
_endloop


s.range("A1:B10").select
c << e.charts.add
c.ChartType << :|xl3DPieExploded|
e.ActiveChart.Location(:|xlLocationAsObject|, "bhimu")
e.Visible << _True

_endblock



if location is not specified, then the chart is added to a new sheet.

3 comments:

  1. Hi Bhimesh,

    Very nice. I copied/pasted the code into my Magik session and got the 3D pie chart in an Excel spreadsheet!

    So for your next Magik Fun challenge, maybe you could modify the the Excel graph code to use the Google Charts API (http://code.google.com/apis/chart/types.html#pie_charts). You could make HTTP requests from Magik that call the Google Charts API and then do something with the returned chart picture. Core Magik doesn't have any easy-to-use classes for making HTTP requests, but if you go to the SourceForge MagikCmpnts community and download http://tinyurl.com/mclib-core-swaf-20080307-zip, you can browse to \modules\mclib_http_connection\examples\yahoo_maps_interface.magik to see an example of how to use the mclib:http_request class for making a request.

    Good luck with your new blog!

    Alfred

    ReplyDelete
  2. i am trying http_connection examples... but as my system is behind the fire walls, it is giving traceback,

    sw_net_acp.get_host_by_nam"weather.yahooapis.com") is raising a condition sayin sw_net_lookup failed....

    ReplyDelete
  3. I think that the http_connection might not handle web server proxies. In that case you could use cURL (http://curl.haxx.se/). This is a command-line executable that you can call from Magik but you can also specify the proxy server username/password. Let me know how it goes.

    ReplyDelete