Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Stupid Chart Tricks

, 2 Apr 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
The new free Chart Control from Microsoft is awesome (for download details see my original post here).  But if you can avoid being dazzled by the plethora of charting features, you may realize it can be used for other tasks. You can use the Chart Control to display simple text dynamically in a bitm

The new free Chart Control from Microsoft is awesome (for download details see my original post here). But if you can avoid being dazzled by the plethora of charting features, you may realize it can be used for other tasks.

You can use the Chart Control to display simple text dynamically in a bitmap.

You don't need to do any plotting to use the control.

  1. Drop a Chart onto a page.
  2. Delete all the sub-elements inside the Chart Element:
        <asp:Chart ID="Chart1" runat="server">
        </asp:Chart>
  3. In the properties windows, add and configure the Titles you wish to display. Make sure to give each title a descriptive name so you can access it from code-behind.

Here is what the demo markup looks like:

    <asp:Chart ID="ChartTextOnly" runat="server" 
        BackColor="Transparent" 
        Height="80px"
        Width="612px" 
        EnableViewState="True">
        <Titles>
            <asp:Title BackColor="ForestGreen" 
                BackGradientStyle="LeftRight" 
                BackSecondaryColor="Blue"
                Font="Forte, 16pt" 
                ForeColor="Yellow" 
                Name="MainTitle" 
                Text="Congratulations, you have logged in successfully!"
                BorderColor="Black" 
                ShadowOffset="5">
            </asp:Title>
            <asp:Title Name="Spacer" 
                BackColor="Transparent"
                Font="Courier New, 4pt" 
                Text=" ">
            </asp:Title>
            <asp:Title Alignment="MiddleLeft" 
                BackColor="Transparent" 
                Font="Lucida Calligraphy, 10pt"
                Name="Date" 
                Text="Star Date" 
                TextStyle="Frame">
            </asp:Title>
        </Titles>
    </asp:Chart>

Here is what the code-behind, that dynamically updates the text, looks like:

    public partial class _Default : System.Web.UI.Page
    {
        string UserName = "Matilda";
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)  // first time only
            {
                ChartTextOnly.Titles["MainTitle"].Text =
                    String.Format("Congratulations {0}, you've logged in successfully!", 
                    UserName);
 
                ChartTextOnly.Titles["Date"].Text = "On Star Date: " + 
                    DateTime.Now.ToLongDateString();
            }
        }

And here is what the final product looks like (pretty sexy, eh?):

I know what the smart people are thinking: "Now hold on there Sparky, sure it looks cool, but if the server has to render a new graphic on every page post-back, my users are going to have me tied to a post, stripped to the waist and horse-whipped."

That's true. And I would be the first in line. So, to avoid rendering the image on every post-back, set the ViewState of the chart to true.

Notes:

This is useful if you need to display text that you don't want people to copy and paste. It's like a poor man's Captcha .

Any font can be used. Since the text is rendered at the server, it will be exact and not dependent upon the client machine's fonts.

I thought the space between the two titles was insufficient so I created a third title between them to be a spacer. The height of the space is controlled by the font size of the empty string.

I hope you find this useful.

Steve Wellens

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Steve Wellens
EndWell Software, Inc.
United States United States
I am an independent contractor/consultant working in the Twin Cities area in Minnesota. I work in .Net, Asp.Net, C#, C++, XML, SQL, Windows Forms, HTML, CSS, etc., etc., etc.

Comments and Discussions

 
GeneralSystem.Drawing namespace PinmemberVincent Curry18-Oct-09 7:40 
GeneralRe: System.Drawing namespace PinmemberSteve Wellens18-Oct-09 11:43 
GeneralRe: System.Drawing namespace PinmemberVincent Curry18-Oct-09 22:03 
GeneralRe: System.Drawing namespace PinmemberSteve Wellens19-Oct-09 3:01 
GeneralRe: System.Drawing namespace PinmemberVincent Curry19-Oct-09 22:49 
GeneralRe: System.Drawing namespace PinmemberSteve Wellens20-Oct-09 3:23 
GeneralRe: System.Drawing namespace PinmemberVincent Curry20-Oct-09 4:48 
GeneralRe: System.Drawing namespace PinmemberSteve Wellens20-Oct-09 5:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141030.1 | Last Updated 2 Apr 2009
Article Copyright 2009 by Steve Wellens
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid