none
C# - Building chart in Word - how to change axis text orientation RRS feed

  • Question

  • Hi,

    I worked out the basics of how to build a graph in C#:

     

    object oClassType = "MSGraph.Chart.8";
    Word.InlineShape oShape = range.InlineShapes.AddOLEObject(ref oClassType);
    Graph.Chart graphChart = oShape.OLEFormat.Object as Graph.Chart;
    graphChart.ChartType = ...;
    ...
    
    graphChart.Application.DataSheet.Cells[i, j] = ...
    

     

    Now the problem is: for every data point to be plotted, its X-value - the text to be displayed on the X-axis is too long, there is not enough space to display every one of them on the X-axis. If I can rotate the text on the X-axis by 90 degrees, or 45 degrees to have a 3D effect, then I will be able to display all the X-values.

    If I confused you - let's say I want to draw a line chart about the sales on the past 24 months. I want to display the year and month of all 24 data points on the X-axis. Obviously if they are displayed horizontally there won't be enough space. But if I display the year-month text vertically there may be.

    So the question is: how to change the text orientation of the two axis

    And before I even get to this question: how do I get the X-axis and Y-axis?




    Friday, August 12, 2011 5:52 AM

Answers

  • As I mentioned in my reply to you the other day, for your other question, it would help us a lot if you'd mention the version of Word/Office you're working with. I know MS Graph basically hasn't changed in almost a decade and is on its way out, but sometimes things do change...

    As you're working with MS Graph, which is an Office-wide applet and not Word-specific the better place for these questions would be the General Office Development forum.

    There are various links on MSDN with information about the object model (depending on the version of Office involved). All of this is VB-code. Here's the most recent, for Office 2010:
    http://msdn.microsoft.com/en-us/library/ff835247.aspx

    I recommend you look up the following terms and properties: Axes, Axis, DisplayUnitLabel, TickLabels, Orientation


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Silan Liu Saturday, August 13, 2011 12:59 PM
    Friday, August 12, 2011 9:18 AM
    Moderator
  • At the moment, there are three ways to create graphs (charts) in Word. You'll find all three in the "Insert object" dialog box and recording a macro will give you the basic syntax (similar to what you have for MS Graph).

    1. MS Graph
    2. Excel Chart (class type Excel.Chart.8) for the charting engine 97-2003
    3. Excel Chart (class type Excel.Chart.12) for the new charting engine introduced in Excel 2007

    MS Graph uses the same engine as (2), but doesn't provide all the same functionality. Most developers detest it, but the positive side is that you can create charts without needing to have Excel installed on the machine.

    (3) has the advantage of being Open XML, which means you can create charts as XML and insert them into closed Office files, rather than needing to "interop" with the Office applications. This is especially relevant if your code should run server-side. Another plus is that the object model has been pulled 100% into the other Office app object models, so you don't need a reference to the Excel library to use it (as is the case with (2)). And it has a lot more "modern" functionality and possibilities than the older engine.

    The basics of using and creating charts is really an Excel-topic. If you dig into the MS Graph object model you'll see that all the Enums are Excel enums. If you create a (2) or (3) chart you'll be automating the Excel application and in the case of (3) it will appear on-screen next to the Word document window. In the case of (2), you'll usually (but not always) get in-place embedding so it won't be as obvious.

    (1) and (2) will work with Office 97 through 2010. (3) only works in Office 2007-2010.

    For (2), you need to look in the Excel object model for versions 97 - 2003 (any will do)

    For (3), start up any Office VBA IDE, press F2 to bring up the object browser, they type Chart. That should pick up most of the object members and from there I think you can find what you need.

    You haven't said what it is you have to do (besides putting a graph in a Word document), but if you're assembling documents from scratch, I very much recommend you look at OpenXMLDeveloper.org


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Silan Liu Sunday, August 14, 2011 3:28 AM
    Saturday, August 13, 2011 2:44 PM
    Moderator

All replies

  • As I mentioned in my reply to you the other day, for your other question, it would help us a lot if you'd mention the version of Word/Office you're working with. I know MS Graph basically hasn't changed in almost a decade and is on its way out, but sometimes things do change...

    As you're working with MS Graph, which is an Office-wide applet and not Word-specific the better place for these questions would be the General Office Development forum.

    There are various links on MSDN with information about the object model (depending on the version of Office involved). All of this is VB-code. Here's the most recent, for Office 2010:
    http://msdn.microsoft.com/en-us/library/ff835247.aspx

    I recommend you look up the following terms and properties: Axes, Axis, DisplayUnitLabel, TickLabels, Orientation


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Silan Liu Saturday, August 13, 2011 12:59 PM
    Friday, August 12, 2011 9:18 AM
    Moderator
  • Thanks Cindy! You said MS Graph is on its way out - I used MS Graph not because its my favorite, but because that's the only way I knew that can draw some graphs in Word. What is the next generation tool that can do it? Can you post a link to that please?

    I am using Office 2010.

    Saturday, August 13, 2011 1:03 PM
  • At the moment, there are three ways to create graphs (charts) in Word. You'll find all three in the "Insert object" dialog box and recording a macro will give you the basic syntax (similar to what you have for MS Graph).

    1. MS Graph
    2. Excel Chart (class type Excel.Chart.8) for the charting engine 97-2003
    3. Excel Chart (class type Excel.Chart.12) for the new charting engine introduced in Excel 2007

    MS Graph uses the same engine as (2), but doesn't provide all the same functionality. Most developers detest it, but the positive side is that you can create charts without needing to have Excel installed on the machine.

    (3) has the advantage of being Open XML, which means you can create charts as XML and insert them into closed Office files, rather than needing to "interop" with the Office applications. This is especially relevant if your code should run server-side. Another plus is that the object model has been pulled 100% into the other Office app object models, so you don't need a reference to the Excel library to use it (as is the case with (2)). And it has a lot more "modern" functionality and possibilities than the older engine.

    The basics of using and creating charts is really an Excel-topic. If you dig into the MS Graph object model you'll see that all the Enums are Excel enums. If you create a (2) or (3) chart you'll be automating the Excel application and in the case of (3) it will appear on-screen next to the Word document window. In the case of (2), you'll usually (but not always) get in-place embedding so it won't be as obvious.

    (1) and (2) will work with Office 97 through 2010. (3) only works in Office 2007-2010.

    For (2), you need to look in the Excel object model for versions 97 - 2003 (any will do)

    For (3), start up any Office VBA IDE, press F2 to bring up the object browser, they type Chart. That should pick up most of the object members and from there I think you can find what you need.

    You haven't said what it is you have to do (besides putting a graph in a Word document), but if you're assembling documents from scratch, I very much recommend you look at OpenXMLDeveloper.org


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Silan Liu Sunday, August 14, 2011 3:28 AM
    Saturday, August 13, 2011 2:44 PM
    Moderator
  • Thanks a lot Cindy for your detailed reply!

    I am writing a simple report generator. We have a well-formatted Word report template with company headers and footers and water marks and all that. My report generator will  interact with user and send various queries to database, and use the returned data to insert tables and charts into different locations in the Word report.

    I just inserted a chart into Word 2010, and the chart looks much prettier than the old-style MS Graph chart I produced, and when the X-axis labels are too long to fit, it automatically rotates them to vertical. So I don't even need to customize the axis! 

    I recorded a macro while doing that, but all that was recorded was only following:

     

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    '
     Selection.InlineShapes.AddChart Type:=-4101
    End Sub
    
    

    I still don't know, using this Excel chart (in place of Microsoft Graph), how do I programmatically draw a line - I know how to do it in MS Graph, as shown in my posted code.

     

    I know I might be asking for too much - can you post a few lines of code just like what I posted? Boss wanted the report quick and there might not be enough time for me to read too much documentations.


    Sunday, August 14, 2011 3:46 AM
  • Hi Cindy,

    I found some demo on DocumentReflector. But I am not planning to learn open XML SDK at the moment. I found out how to rotate the X-axis label on the MS Graph which I sort of know - thanks for your earlier posting. So please ignore my previous post asking for open XML sample code. Thanks again for your great help! 


    Sunday, August 14, 2011 5:09 AM
  • "Boss wants it quick" - Yep, no time to learn an entirely different technology :-) Glad you've got things working for the task at-hand!

    But now you know it's there and I'm guessing it may well come in handy, somewhere down the line.

    If you get into the Open XML at some point and need assistance, the forums at OpenXMLDeveloper.org and the Open XML SDK forum on MSDN are the right places to ask. I know what it should be able to do, appreciate the capabilities fully, can read the files, but...

    ...haven't had time to delve into actually doing anything with it. The other forums are specialized, so the better place to ask.


    Cindy Meister, VSTO/Word MVP
    Sunday, August 14, 2011 7:29 AM
    Moderator