This topic is locked
[SOLVED]

Multiple lines on a chart?

2/24/2024 5:49:05 AM
ASPRunner.NET General questions
D
david powell author

I am sure I am being a bit slow about this.
I am trying to create a line chart, with multiple lines showing total valuations for a number of locations, with the y axis being months.
SQL query looks like this:

SELECT
location_name,
pricenumberpacks as value,
created
FROM inventory
UNION ALL
SELECT
location_name,
price
numberpacks as value,
created
from inventory_archive

Having to join two tables, works fine and gives an output like this:
Site 1 45454 1/10/2023
Site 2 3433 1/10/2023

I attach the config screns for the graph below, but I cant see how to tell asprunner to create a different line for each location. It reutrns a graph with just one line.

What am I doing wrong or missing
img alt

img alt

img alt
?

D
david powell author 2/24/2024

Addendum: I do realise that if I have multiple columns each can be ascribed to a data series. But that would involve with the data I have doing a cross-tab which is not supported by mysql, and I cant do the #case trick because I dont know how many different sites there will be.

I am tempted ot convert to sql server which does support cross tabs, but I am not sure if asprunner will be able to handle them anyway so am reluctant to do this quite yet...

D
david powell author 2/24/2024

OK, I have created a workaround . Written an sql script that creates a new table as a cross tab with a column for any entries with a specific 'location'. I can then use this table, which is essentially a pivot/crosstab table, a ssource for the chart.

This script contains a union to link a current/archive sourcce table, and does a calculation as well on each row. Run as a cron job on the server.

-- Set sql_mode to ignore zero dates
SET sql_mode = 'NO_ZERO_DATE,NO_ZERO_IN_DATE';

-- Drop the table if it exists
DROP TABLE IF EXISTS inventory_crosstab;

-- Generate dynamic SQL for pivot
SET @sql_created = NULL;
SET @sql_location = NULL;

SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(CASE WHEN SUBSTRING(location_name, 1, 3) = ''',
SUBSTRING(location_name, 1, 3),
''' THEN price * numberpacks END) AS ',
SUBSTRING(location_name, 1, 3) -- Use location_name substrings as column names
)
) INTO @sql_location
FROM (
SELECT location_name FROM inventory
UNION
SELECT location_name FROM inventory_archive
) AS combined_tables;

-- Create the new table inventory_crosstab
SET @sql_created = 'MAX(created) AS created';

SET @sql = CONCAT('CREATE TABLE inventory_crosstab AS SELECT id, ', @sql_created, ', ', @sql_location, ' FROM (SELECT FROM inventory UNION ALL SELECT FROM inventory_archive) AS combined_tables GROUP BY id;');

-- Prepare and execute the dynamic SQL
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Reset sql_mode
SET sql_mode = '';

D
david powell author 2/25/2024

Only problem with this approach is that the charts are static intheir construction, each data item has to be allocated to a value in the query.

What would be great is if it could be dynamic, and allow all items in a collection each to be assigned a line, so that it can cope with an unknown number of 'columns'.