简体   繁体   English

在Excel图表上切换分组轴

[英]Switch grouped axis on Excel chart

I have a chart of type xlColumnClustered and I want to switch the x and y axis. 我有一个xlColumnClustered类型的图表,我想切换x和y轴。 Problem is that the axis is grouped. 问题是轴是分组的。

My data looks like this: 我的数据如下:

Country Permenant   Flat        1%
                    Appartment  0%
                    House       2%
                    Shaq        0%
        Temporary   Flat        2%
                    Appartment  9%
                    House       1%
                    Shaq        0%
State   Permenant   Flat        8%
                    Appartment  13%
                    House       30%
                    Shaq        5%
        Temporary   Flat        6%
                    Appartment  0%
                    House       2%
                    Shaq        0%

In ggplot the command I would need is coord_flip(), it does this, does vba have any functionality similar to this?: ggplot中我需要的命令是coord_flip(),它是这样做的,vba有没有类似的功能?:

在此输入图像描述

My code currently looks like this: 我的代码目前看起来像这样:

Sub insert_housing()

    Dim strRange As String
    Dim rngChart As Range
    Dim myChart As Chart

    'Just grabbing the data, nothing to see here
    sheet_name = "Sheet2"

    With Sheets(sheet_name)
        StartRow = .Range("D10000").End(xlUp).End(xlUp).Row
        EndRow = .Range("D10000").End(xlUp).Row
    End With

    Sheets(sheet_name).Activate
    Sheets(sheet_name).Range("$A$" & CStr(StartRow) & ":$D$" & CStr(EndRow)).Select

    'Probably start reading here
    Set myChart = ActiveSheet.Shapes.AddChart(xlColumnClustered).Chart

    With myChart
        .ChartArea.Format.TextFrame2.TextRange.Font.Size = 6
        .HasTitle = True
        .HasAxis(xlCategory, xlPrimary) = True
        .ChartTitle.Text = "Frustrating Graphic of Housing"
        .SeriesCollection(1).Name = "stuff"

        .HasAxis(xlValue, xlPrimary) = True
        .Axes(xlCategory, xlPrimary).CategoryType = xlAutomatic
    End With

End Sub

So to be clear, I'd like to see the grouped x axis on the y axis, y axis on the x axis. 所以要清楚,我想看到y轴上的分组x轴,x轴上的y轴。

What have i tried? 我试过了什么?

  • Switch Row/column (this just gives me catagories such as "Country Permenant Flat" in the legend, and a single bar for this, it doesn't visually rotate the graph) 切换行/列(这只是给我一个类别,例如图例中的“Country Permenant Flat”,以及一个单独的栏,它不会在视觉上旋转图形)
  • Playing with the data source and trying to manually adjust the axis 使用数据源并尝试手动调整轴
  • The accepted solution here 接受的解决方案在这里
  • I've also trawled numerous other sites in an attempt to solve this 为了解决这个问题,我还搜遍了许多其他网站

Can anyone shed any light on how to put a grouped axis on the y axis in Excel VBA? 任何人都可以阐明如何在X V中将分组轴放在y轴上吗?

Can you not change chart type and use 2D plot? 你能改变图表类型并使用2D图吗?

This would yield: 这会产生:

数据

Code recorded for this was: 为此记录的代码是:

ActiveSheet.Shapes.AddChart2(216, xlBarClustered).Select
ActiveChart.SetSourceData Source:=Range("Sheet6!$A$1:$D$16")

Which you can tidy into something like the following, to get rid of the .Select 你可以整理下面这样的东西,摆脱.Select

Dim c As Chart
Set c = ActiveSheet.Shapes.AddChart2(216, xlBarClustered).Chart
ActiveChart.SetSourceData Source:=Range("Sheet6!$A$1:$D$16")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM