Stacked Manhattan 3D Bar chart

Need help in implementing some specific function to your LightningChart Ultimate powered application? Post a question and get code snippets from other LightningChart Ultimate community members.

Moderator: Arction_LasseP

User avatar
ArctionPasi
Posts: 1366
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland

Stacked Manhattan 3D Bar chart

Post by ArctionPasi » Wed Jul 27, 2016 12:53 pm

I made a simple demo how to make stacked manhattan bar chart, by utilizing Polygon3D objects of View3D, and sharing this sample with you ;)

LightningChart has Manhattan view out-of-the-box but stacked visualization must be made with a workaround.

The sample is made with v.7 Semi-bindable WPF chart.

Empty window with just a grid on it:

Code: Select all

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="gridMain">
       
    </Grid>
</Window>

And the actual code:

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Arction.Wpf.SemibindableCharting.Axes;
using Arction.Wpf.SemibindableCharting;
using Arction.Wpf.SemibindableCharting.Views;
using Arction.Wpf.SemibindableCharting.Views.View3D;
using Arction.Wpf.SemibindableCharting.Series3D; 


namespace WpfApplication1
{
    /// <summary>
    /// Shows how to make stacked manhattan chart with PolygonSeries3D 
    /// </summary>
    public partial class MainWindow : Window
    {

        private LightningChartUltimate _chart;
        private List<Bar> _listBars = new List<Bar>();

        
        private const int ColCount = 20;
        private const int RowCount = 15;
        private const int StackCount = 4;

        public MainWindow()
        {
            InitializeComponent();
            CreateBarData(); 
            CreateChart(); 
        }

        void CreateBarData()
        {
           
            //Create bar data 
            Random rand = new Random();
            
            for (int colIndex = 0; colIndex < ColCount; colIndex++)
            {
                for (int rowIndex = 0; rowIndex < RowCount; rowIndex++)
                {
                    for (int stackIndex = 0; stackIndex < StackCount; stackIndex++)
                    {
                        Bar bar = new Bar(colIndex, rowIndex, stackIndex, 5 + rand.Next(0, 10));
                        _listBars.Add(bar);
                    }
                }
            }

        } 

        
        void CreateChart()
        {
            
            _chart = new LightningChartUltimate();
            _chart.ActiveView = ActiveView.View3D;

            View3D v = _chart.View3D;
            const double MinX = 0; 
            const double StepX = 10; 
            const double MinZ = 0; 
            const double StepZ = 10;
            const double SizeX = 3;
            const double SizeZ = 3;

            v.Lights = View3D.CreateDefaultLights(); 


            foreach(Bar bar in _listBars)
            {
                double x = MinX + bar.ColumnIndex * StepX;
                double z = MinZ + bar.RowIndex * StepZ;
                Color c = DefaultColors.SeriesWpf[bar.StackIndex % DefaultColors.SeriesWpf.Length]; 
                double yMin,yMax; 
                SolveYMinMax(bar, out yMin, out yMax); 
                CreatePolygon3D(x, z, yMin, yMax, c, SizeX, SizeZ); 

            }

            v.XAxisPrimary3D.SetRange(MinX-StepX, MinX + ColCount * StepX);
            v.YAxisPrimary3D.SetRange(0, 100); 
            v.ZAxisPrimary3D.SetRange(MinZ-StepZ, MinZ + RowCount * StepZ); 

            gridMain.Children.Add(_chart); 
        }


        Polygon3D CreatePolygon3D(double xCenter, double zCenter, double yMin, double yMax, Color color, double sizeX, double sizeZ)
        {

            Polygon3D polygon = new Polygon3D(_chart.View3D, Axis3DBinding.Primary, Axis3DBinding.Primary, Axis3DBinding.Primary);
            polygon.Material.DiffuseColor = color; 

            polygon.Points = new Polygon3DPoint[] { 
                new Polygon3DPoint(xCenter - sizeX/2.0, zCenter - sizeZ/2.0), 
                new Polygon3DPoint(xCenter - sizeX/2.0, zCenter + sizeZ/2.0), 
                new Polygon3DPoint(xCenter + sizeX/2.0, zCenter + sizeZ/2.0), 
                new Polygon3DPoint(xCenter + sizeX/2.0, zCenter - sizeZ/2.0)};
            polygon.YMin = yMin; 
            polygon.YMax = yMax; 

            _chart.View3D.Polygons.Add(polygon);
            return polygon; 
        }


        void SolveYMinMax(Bar bar, out double yMin, out double yMax)
        {
            //Seek bars with same col and row index, and having stack index lower than given stack index. 
            //Then we can calculate the bottom of the bar  

            yMin = 0;
            yMax = 0;
            IEnumerable<Bar> barsWithSamePosition = _listBars.Where(b =>
                b.ColumnIndex == bar.ColumnIndex
                && b.RowIndex == bar.RowIndex
                && b.StackIndex < bar.StackIndex
                );


            double sum = barsWithSamePosition.Sum(b => b.Value); 
            
            yMin = sum;
            yMax = sum + bar.Value;
            
        } 

        
    }

    public class Bar
    {
        public int RowIndex;
        public int ColumnIndex;
        public int StackIndex; 
        public double Value;
        public Bar(int columnIndex, int rowIndex, int stackIndex, double value)
        {
            RowIndex = rowIndex;
            ColumnIndex = columnIndex;
            StackIndex = stackIndex; 
            Value = value;
        }
    }
}
It produces a chart like this:
Stacked Manhattan 3D Bar Chart
Stacked Manhattan 3D Bar Chart
StackedManhattanBarChart.jpg (199.04 KiB) Viewed 7188 times
I hope this helps you guys. :geek:
LightningChart Support Team, PT