Programmatically creating multilevel axis chart with VBA on Excel - vba

I have some code that retrieves data from a SAS Table on a server. I'm trying to chart this data using VBA, without copying it to an Worksheet first (Feeding the recordset directly into the charts)
I've had success so far with this. The problem is, my charts need to have multilevel categorical X axis, and i'm having trouble automating this only with VBA
What I'm trying to get is something like this
http://imgur.com/ElwHwjE (I can't post the image due to lack of reputation)
But so far all my attempts lead to this
http://imgur.com/id7Vua0
I know I can do this if I first make a Pivot Table from my recordset and then build a chart on top of it, but as stated before I'm trying to do this without copying the data into a Sheet.
Also tried messing with TickLabel options (Multilevel and Depth), but so far to no avail.

I've tried everything I could think of, and I have not been able to create a multiple level categorical axis without having the axis data in a worksheet. I've tried various 1D and 2D arrays, with array elements that include various LF, CR, and CRLF characters. I've tried using the worksheet range, then converting XValues to an array, and of course, this broke the axis without providing a usable array.
Bottom line: Excel charts were designed to plot worksheet data, and you can't always get around this requirement. Most of the time I think you shouldn't waste time trying to get around it.

Related

Formatting Data in excel sheet with blue prism

I'm trying to run a duplicate check In which varying data is pulled from a website and compared to a master list, the master list being stored in Excel. The information from the website is read from a table in which has line breaks. These breaks are translated over to the data collection they are initially stored in. Some of the data from the website us eventually written to the master list in Excel. So when I read the master list back into Blue Prism to run a duplicate check, the rows that have line breaks are written into a collection as multiple rows (ex. I should have on 7 rows in my collections but am getting 42). Since the rows are not EXACTLY the same between the 2 collections, when it runs the automation does not recognize the duplicates.
The easiest way to solve this would be if I could make the collection rows have no line breaks as soon as the data is read. I've attempted to use the calculation stage to do so with no luck. I'm not sure if it is actually possible to do this, but would appreciate any direction.
Record an Excel macro to do the data sorting/cleaning in Excel (possibly Text To Columns, etc..) and then include the running of the macro as part of your Blue Prism process by using an action stage and the MS Excel VBO - Run Macro. Get the process to create an Excel instance (and create a handle data item from that stage), then use Open Workbook (whatever workbook you store your Macro in) and then use the MS Excel VBO - Run Macro (use the same handle created earlier and type in the name of the "macro").
It sounds like what is happening is that the MS Excel VBO is grabbing the data from the Excel Worksheet wholesale.
This is to say that it's accessing your Worksheet table, copying the cell values BUT not the cell formatting data, and then dumping the values into a BP collection.
Since it did not bring along any of the original cell formatting data to reference when it went to populate the collection it's just breaking up the values based on crturn/line breaks. Thus, your collection is organized based on that, and not on the original Worksheet cell.
So, with that said, on to a solution!
Solution 1
Brute force the organization of the incoming Excel cell data to the collection by looping over the Excel Worksheet cell-by-cell.
Run a loop, and in that loop have BP go into the Excel Worksheet and grab the first populated cell it comes across. Run a formatting/cleanup Calculation stage over the data. Dump the cell value into a single collection field.
Repeat.
This is...inelegant, expensive at best, and not at all recommended for any medium to large dataset. But it's definitely the best way to do string manipulation and value comparisons before it hits your collection. Since it sounds like your using a Master template then you as-well know what the expected format of your data should be.
This method will enable you implement Trim(), Concat(), or Split() in a Calculation stage to better organize your incoming data before you dump it into a collection.
This is also basically what I think you're already trying to do, but cell-by-cell instead of Worksheet row-by-row or table-by-table.
Solution 2
Clean up the table data you grab from the website before you dump it into the Excel Worksheet.
This is basically Solution 1, but in reverse. Simply format/cleanup your data before it hits you Excel Worksheet.
I'm not sure this is any better than Solution 1, but, you know, it's something...
Solution 3
Format the cell data IN the MS Excel Worksheet itself.
Basically rearrange the cells and cell data in the Excel Worksheet into a more predictable format by using the Split, Trim, Merge, or other actions included in the MS Excel VBO. You can also do this using the Data - OLEDB utility object, but that requires some pretty solid understanding of SQL syntax.
This would look like this using the MS Excel VBO:
Grab the Excel Worksheet data wholesale and dump into a collection
Count the rows/fields of the collection
Is that number consistent with the desired/expected format of your data?
If not, have the bot go back into the Excel Worksheet and reformat the cells by removing any carriage returns/line breaks/whatever else
Repeat.
However, I'm always reluctant to reformat any original source, as it's then hard to figure out what wrong and where it went wrong when you've changed the original structure of your data. So it's best to always make a copy of the Worksheet before you make any manipulation.
Unfortunately I don't have access to my BP environment at the moment or I'd provide you with the act object actions you'd need to do any of this, my bad. Once I do I'll update this answer.

How to mimic range and chart activity using VBA Excel

The basic data set looks like this:
FIG 1
What a pre-decessor did with the data set is to make it linear by copying all the data, i.e. saturday -> data, sunday -> data
Like this:
FIG 2
Using this format, he was able to generate a graph like this:
FIG 3
Now, I have started to automate this document, where now, I have got to the stage of automating Fig 1.
My question is, by looking at the graph, is there a way to skip FIG 2 and just generate the graph using the data in FIG 1?
I have spent a couple of hours thinking about this and I cant seem to think of a solution.
I was thinking along the lines of creating a data range with, 00:00, 01:00 ..... and the day data, iterating that for all the days then combining the range to make a mega range, but I can't see how that will work.
Can anyone push me in the right direction to solve this problem?
Thanks
Chris
The only solution I can think of would be to write some vba which creates the fig 1 format from the original format. I'm fairly certain Excel doesn't allow you to draw the chart you want from the data in the original format but the vba should be quite simple.
For readability I'd recommend you transpose the fig 2 data so that it is going down rather than up. The vba should traverse the data and write the chart data rows, then update the chart data source to use your finished range.
That's assuming the number of periods will be changing. If they won't you can just use formulae to rearrange the data and no vba will be required.

bind Data table to chart in vb

I have a project that im working on at the moment in VB.
Basically I have a Data Table in VB it populates with 4 columns and a variable number of rows, the column names are as follows;
Gear, RPM, Speed, CO2
The data table appears in datagridview correctly but that's as far as I have gotten :/
What I am trying to do is to make a line chart called mainGraph to draw a graph based on these results, I'm getting stuck when trying to populate the chart.
ANY thoughts would be really appreciated.
I am guessing you are using the ms chart controls and not some 3rd party like Telerik.
This should be usefull http://blogs.msdn.com/b/alexgor/archive/2009/02/21/data-binding-ms-chart-control.aspx
Also, there is source code found at http://archive.msdn.microsoft.com/mschart/Release/ProjectReleases.aspx?ReleaseId=4418
in short this is a way to get things displayed
mainGraph.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
mainGraph.Series(0).Points.DataBind(yourDataTable.DefaultView, "Speed", yourDataTable.DefaultView, "Gear")
These commands should make a line graph with Speed on your x axis, and Gear on your Y. Assigned to "Series(0)"
To add more columns, you need to add more "Series"
mainGraph.Series.Add("RPM")
Do some thinking about how you want this data displayed.
how would you like to set this graph up?
I'm assuming you would want a line graph?
What is going to be the X axis? Is there a missing column that needs to be "time" in the data table?
do you want them all on one graph/chart area?
If you would like more charts, then you need to add chart areas. A series can be assigned to a chart area.
Hopefully this helps.

Automate graph generation based on variable time

I need help creating an Excel macro.
I want to take the Excel file here and create a graph of each of the servers with time being the x axis and memory utilizaiton % being the y axis. The problem is, the number of data points for each server can be variable (sometimes the first server has x number of data points, other times it has x+2).
Can anyone help me with this?
Sure. If I'm understanding correctly, you need to create a chart where your data can grow in size. In that case you need to use a "dynamic named range" that expands as new data is added. The chart that you will end up creating is called a "dynamic chart". The dynamic range is set up using the OFFSET function which allows it to expand.
I think these videos will help you:
Excel 2010 / 2007: http://www.youtube.com/watch?v=M9hhUVljTx4
Excel other: http://www.youtube.com/watch?v=7le-m8YRP6M

Add Chart Data from other Workbooks into one series with VBA - Excel 2007

I need to create a Chart which will grab data from external sources when a macro is run. Setting up the UserForm and all that is fine, all done. However, I can't find out how to add another piece of the series.
Is it even possible to have a series that sources data from several different locations? Basically I need it to go in chronological order (Horizontal Axis are all dates), but each date comes from a different workbook entirely. I have tried to get Excel to do this with its basic functions. I just can't get it to put it into the graph. If possible, I would like to not have to copy all the reference data to the workbook with the graphs, as it is already going to have 16+ Charts that will be on their own sheet.
Does anyone know how to make Excel accept many different references into one series? How it would be done with VBA? I'm not too bad with VBA, just haven't had to deal with Charting before.
You cannot combine data from different worksheets into the values of a series, or into its Xvalues. (You can use data from one sheet for X and another for Y).
I recommend setting up a chart data worksheet, then copy the data from each of your data sheets and pasting it in this sheet.
The problem with using arrays for the data is that the length of the array is limited to around 250 characters. Including commas and assuming only a 2 digit number with no decimal digits, this means you get only 80 values. If you have floating point values, you will be lucky to get more than about 15 values.
I don't think you can have a single axis of a single series sourced from multiple ranges/addresses, but you can write VBA to set the values of a chart series to specific values rather than to a contiguous range. Here's an example from the help:
Charts("Chart1").SeriesCollection(1).Values = Array(1, 3, 5, 7, 11, 13, 17, 19)
Charts("Chart1").SeriesCollection(1).XValues = Array(5.0, 6.3, 12.6, 28, 50)
You would have to write code to read the values out of the ranges in your different workbooks, but this should work.