Click here to Skip to main content
15,885,890 members
Articles / Web Development / ASP.NET

Stupid Chart Tricks

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
2 Apr 2009CPOL2 min read 31.3K   14   8
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.

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.NET
    <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.NET
<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:

C#
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

This article was originally posted at http://weblogs.asp.net/stevewellens/privaterss.aspx

License

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


Written By
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 Pin
Vincent Curry18-Oct-09 7:40
professionalVincent Curry18-Oct-09 7:40 
GeneralRe: System.Drawing namespace Pin
Steve Wellens18-Oct-09 11:43
Steve Wellens18-Oct-09 11:43 
GeneralRe: System.Drawing namespace Pin
Vincent Curry18-Oct-09 22:03
professionalVincent Curry18-Oct-09 22:03 
GeneralRe: System.Drawing namespace Pin
Steve Wellens19-Oct-09 3:01
Steve Wellens19-Oct-09 3:01 
GeneralRe: System.Drawing namespace Pin
Vincent Curry19-Oct-09 22:49
professionalVincent Curry19-Oct-09 22:49 
GeneralRe: System.Drawing namespace Pin
Steve Wellens20-Oct-09 3:23
Steve Wellens20-Oct-09 3:23 
GeneralRe: System.Drawing namespace Pin
Vincent Curry20-Oct-09 4:48
professionalVincent Curry20-Oct-09 4:48 
GeneralRe: System.Drawing namespace Pin
Steve Wellens20-Oct-09 5:11
Steve Wellens20-Oct-09 5:11 

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

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