I can already populate the data into the spreadsheet, create the chart based on the range I select. However, when it comes to formatting the actual chart I feel a bit lost as their are so many options!!
It is an xlCylinderBarStacked type chart. I simply need to make the color of the bars a nice light orange and make the background a light blue fading into white at the bottom.
Any idea's how to do this?
Just to close this question off. I played around a little with the properties and the following achieved the gradient effect on the background of the chart.
xlChart.Interior.Color = ColorTranslator.ToOle(Color.LightSkyBlue);
chart.ChartArea.Fill.TwoColorGradient(
Microsoft.Office.Core.MsoGradientStyle.msoGradientHorizontal,
1);
One good trick with Excel and other VBA-enabled apps is to manually create the formatting/content you require using the Excel GUI, whist recording a 'macro'. Once this is done you can then inspect the generated VBA to see how to use the API to acheive the same results programmatically. You will of course have to do some translation from VBA to C# but essentially the same methods should work.
Related
I am trying to create a chart with Excel API in C#. It is going really well, but I don't have some fields.
For example:
Format Excel Chart Background using C#
At this question, James' answer is something I'm looking for but:
The object does not have an Interior property.
And again:
Chart Properties
I see something like ChartColor:
But my charts don't have it.
I though it depends on the type of the chart, but it is just defined inside and doesn't affect the fields (tried to do something like casting, but it was just 'not-smart' heroic try).
I also tried to do an example of chart and just ascribe all attributes, but still there is nothing like Color.
I don't want a background image, just a background hue!
I have the latest Microsoft.Office.Interop.Excel library, but tried on older ones and effect is the same.
Where is the problem?
P.S.
I'm also working with copying all attributes of chart except data, name and axis names. If someone of You did something like that, I would be grateful for any information.
EDIT:
Fixed it just by load example chart and use SetSourceData. But why I can't do it programmatically?
"Where is ChartColor?": It's probably just an Intellisense bug. Usually happens when I sleep the computer too many times without closing VS. Try closing and reopening VS. Also, have you tried just running the code with chart.ChartColor?
"There's nothing like interior": Interior refers to an sub-area that can be formatted. The chart object itself isn't what you want to color. It's the ChartArea that contains Interior, which has a ColorIndex, so try
Excel.Interior interior = chart.ChartArea.Interior;
I'm sorry that wasn't clear in Format Excel Chart Background using C#. You'll need to be wary of posters that answer their own questions. Sometimes, it's ok, but more often, it's "I fixed it with this line" which doesn't help anyone else when taken out of context.
"Where's my problem?": It depends on your implementation. Including some example code helps tremendously on StackOverflow. Some are looking to create a chart as part of a worksheet, like:
Excel.Shape shape = worksheet.Shapes.AddChart2();
Excel.Chart chart = shape.Chart;
Or some developers want to create a chart as a new sheet, like this:
Excel.Chart chart = workbook.Charts.Add();
These are both charts, but behave differently because of their implementation.
How do I set the background color of a piece of text in a PDF document using iTextSharp without taking a form field?
The answer in this post uses a FormField, which according to me is an overkill and too long-winded a way to do something really simple.
Is there a simple way of coloring the background of a piece of text?
You can use the method SetBackground that is available in the Chunk class. There are two variations of this method: one that takes default padding and one that allows you to change the padding.
If you use the onGenericTag() method on a Chunk, you can draw a custom background (and do much more). For instance: you'd use onGenericTag() if you want to draw a rectangle with rounded corners. See my answer to your duplicate question Draw a rectangle at the *current position* and then get its position coordinates
After some trying, I have come to the conclusion that there are 3 ways to do this other than using the FormField (which is the fourth way and how to do that is already linked in the question):
1) Judging from this answer to another similar question, it appears as though there is no concept of a background color for text in the PDF specification. Therefore, one has to draw a rectangle at an absolute position before drawing the text (at that position).
This is like drawing on a Win32 DeviceContext.
2) You can draw a table and set the background color of the cell in which you want a background color.
3) You can write a chunk. The Chunk class has a method named SetBackground(). This doesn't look very nice because it doesn't let you control the padding around the text and between the borders of the box. You can control how far above the baseline the bottom of the text will appear by calling the chunk.SetTextRise(float f) method but that's about it. Still, it's a fast and easy way to get things done if you don't want too much beautification.
Roughly speaking, I want to create a child class from Excel Chart to add the following features into the child:
add a button to the visual appearance of the chart (the button should have a fixed position relatively to the chart - so if I move chart, the button moves as well). Actually, it doesn't have to be exactly button - it may be anything else which allows clicking on it (or do any other action) and do some activities after the click.
add some additional properties to the chart (these properties doesn't appear vizually but they influence the representation of the time-series). Maybe there is a standard way to customize the set of the properties of Excel Chart?
I don't know to what extent it is possible. Maybe "child class" is not that good idea to serve such simple needs. Maybe VSTO has something, but I can't find anything suitable in the Internet.
Any help will be very much appreciated!
PS
For the subquestion #2 I've decided to use the field TAG to put there an object with additional parameters.
In answer to your first question it is possible to embed a form-control button or a shape in a chart, and have it keep its position relative to the chart area, by cutting and pasting it onto the chart. Use the OnAction property to assign a procedure to the button.
Recorded code (in Excel) is like this:
Sub Macro1()
ActiveSheet.Buttons.Add(166.5, 48, 48, 32.25).Select
Selection.OnAction = ActiveWorkbook.Name & "!TestMacro"
Selection.Cut
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Paste
Selection.ShapeRange.IncrementLeft 10
Selection.ShapeRange.IncrementTop 30
End Sub
It may be possibly to do this programmatically without cutting and pasting, perhaps by adding to the ChartObject's ShapeRange, but I haven't explored this.
I do not know to what extent you can directly subclass an Excel Chart (if at all) and suspect that you might just create a wrapper class for it in C#.
How can we add a striped line to an Excel chart using C#?
I am able to create the chart using c# but I didn't get any clue how to add a striped line.
Please make some suggestions.
Thanks
Try this:
- add a data series with a steady value ( of 50 in your case )
- move the new data series to the secondary Y axis
- synchronize the maximum for both Y axes
- set Chart Type for the new data series to Line
- set the Line Style for the new data series to a dash type
I know this is not C#, but it is C# as much as you question is :)
When I have things like this, what I'm usually doing is I "manually" do all I have to do in Excel recording a macro with all the stuff. Then I just analyze the macro. You will find a lot of useful information there.
Good luck!
I have seen many other samples out there that draw smooth text on glass. But I can't use them. I need every single label that gets added at runtime to be smooth. I can't just "draw" text onto the screen.
Is this at all possible, and are there and sources around?
Thank you
Take a long at this article http://msdn.microsoft.com/en-us/magazine/cc163435.aspx#S6
It's a bit long but it answers alot of your question and alore more in regards to glass.
but the relevant part for you directly is
One particular gotcha is that
rendering a GDI item in black uses the
bit pattern 0x00000000-which also
happens to be a completely transparent
black if you are using an alpha
channel. This means that if you draw
with a black GDI brush or pen you'll
get a transparent color, not a black
one. The biggest problem this presents
is when you try to use the default
text color in a control of a text
label that sits on the glass area.
Since the default text color is
usually black, the DWM will consider
this to be transparent and the text
will be written in the glass
incorrectly. An example can be seen in
Figure 10. The first line is written
with GDI+, the second is a text label
control using the default color. As
you can see, it's nearly illegible
because it's actually incorrectly
rendered text that shows up as gray,
not black.
Happily, there are a number of ways
around this problem. Using owner-draw
controls is one. Rendering to a bitmap
that has an alpha channel is another.
Fortunately, the easiest way to get
text on controls is to let the .NET
Framework 2.0 use GDI+ for you. This
is easily accomplished by setting the
UseCompatibleTextRendering property on
your controls. By default, this
property is set to false so that
controls written for previous versions
of the .NET Framework will render the
same. But if you set it to true, your
text will come out looking correct.
You can set the property globally with
the
Application.SetUseCompatibleTextRenderingDefault
method.
He also provides example code you can place in your Main()
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(true);
Application.Run(new GlassForm());
}
But I recommend reading the article, It'll clear up alot of what's going on with Aero/Glass
Cheers,
Phyx