【问题标题】:Kendo bar chart with texture pattern具有纹理图案的剑道条形图
【发布时间】:2016-03-15 13:20:04
【问题描述】:

我有一个如下的剑道条形图。但我需要显示为一些线或点图案,而不是颜色。有人可以帮我解决这个问题吗?

我有图表的数据源。然后我将该数据绑定到数据源。我将如何分配模式?你能帮我解决这个问题吗

 $("#NumActivitiesChart").kendoChart({
        title: {
            text: "Number of Activities Report",
            font: "bold 20px Arial,Helvetica,sans-serif",
            color: "brown"
        },
        //plotArea: {
        //    background: "#EAEFFA"
        //},
        dataSource: dsNumActivitiesData,
        seriesColors: colours,
        series: [{
            type: "column",
            categoryField: "ChartByName",
            field:"NumTestInstances",
            gap:5
        }],

        valueAxis: {
            title: {
                text: "Number of Activities",
                font: "bold 15px Arial,Helvetica,sans-serif",
                color: "brown"
            }
        },

        categoryAxis:{
            title: {
                text: "@Model.Filters.NumActivitiesChartBy",
                font: "bold 18px Arial,Helvetica,sans-serif",
                color: "brown"
            }
        },
        tooltip: {
            visible: true,
            template: "${series.name} : ${value}"
        }
    });

【问题讨论】:

    标签: kendo-ui kendo-chart


    【解决方案1】:

    您可以使用 SVG patterns 将阴影应用为背景。

    在您的 HTML 标记中,定义一个包含您要使用的模式的 svg 元素,例如:

     <div style="position: absolute; width: 0; height: 0;">
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
          <defs>
            <pattern id="pattern1" width="4" height="4" patternTransform="rotate(45 0 0)" patternUnits="userSpaceOnUse">
              <line x1="0" y1="0" x2="0" y2="2" style="stroke:black; stroke-width:1" />
            </pattern>
    
            <pattern id="pattern2" width="4" height="2" patternTransform="rotate(45 0 0)" patternUnits="userSpaceOnUse">
              <line x1="0" y1="0" x2="0" y2="2" style="stroke:black; stroke-width:1" />
            </pattern>
          </defs>
        </svg>
    </div>
    

    然后,在创建图表时,我为每个系列赋予一种独特的颜色,这样我就可以通过该填充颜色轻松获取条形的 SVG 路径。在图表的渲染中,我得到了条形并将填充更改为所需的模式。

            $("#chart").kendoChart({
                theme: "flat",
                seriesDefaults: {
                    type: "column",
                },
                series: [{
                    name: "S1",
                    color: "rgba(150,150,150,0.999)",
                    data: [10, 30, 20, 45, 60]
                },{
                    name: "S2",
                    color: "rgba(120,120,120,0.999)",
                    data: [20, 10, 14, 56, 89]
                } ],
                valueAxis: {
                    majorGridLines: {
                        visible: false
                    },
                },
                categoryAxis: {
                    categories: ["Jan", "Feb", "Mar", "Apr", "May"],
                    majorGridLines: {
                        visible: false
                    },
                },
               render: function(e){                
                  $('[fill="rgba(150,150,150,0.999)"]').attr("fill", "url(#pattern1)");
                  $('[fill="rgba(120,120,120,0.999)"]').attr("fill", "url(#pattern2)");
                }
            });
    

    DEMO

    更新:

    您实际上可以直接将系列颜色设置为图案并避免渲染事件:

            $("#chart").kendoChart({
                theme: "flat",
                seriesDefaults: {
                    type: "column",
                },
                series: [{
                    name: "S1",
                    color: "url(#pattern1)",
                    data: [10, 30, 20, 45, 60]
                },{
                    name: "S2",
                    color: "url(#pattern2)",
                    data: [20, 10, 14, 56, 89]
                } ],
                valueAxis: {
                    majorGridLines: {
                        visible: false
                    },
                },
                categoryAxis: {
                    categories: ["Jan", "Feb", "Mar", "Apr", "May"],
                    majorGridLines: {
                        visible: false
                    },
                },
            });
    

    更新DEMO

    【讨论】:

    • 非常感谢 Ezanker 感谢您的帮助
    • @SamanthKolisetty,其实你可以去掉渲染,直接把图案设置为系列颜色:jsfiddle.net/ezanker/0nmfz6cd/4
    • Inned 一个 morwe 的东西,但系列列是动态的我如何分配模式?
    • @SamanthKolisetty,只要页面上已有图案,然后在生成系列时将图案分配给颜色属性。
    • 嗨,Ezankar,我已经更新了我的代码,你能帮我如何在我的示例中分配模式
    【解决方案2】:

    您可以创建折线图,而不是修改条形图。两个图表都可以使用相同的数据源,因此您唯一需要更改的是设置SeriesDefaults

    @(Html.Kendo().Chart()
        .Name("chart")
        .Title("Site Visitors Stats \n /thousands/")
        .Legend(legend => legend
            .Visible(false)
        )
        .ChartArea(chartArea => chartArea
            .Background("transparent")
        )
        .SeriesDefaults(seriesDefaults => 
            seriesDefaults.Line().Style(ChartLineStyle.Smooth)
        )
        .Series(series => {
            series.Bar(new double[] { 56000, 63000, 74000, 91000, 117000, 138000 }).Name("Total Visits");
            series.Bar(new double[] { 52000, 34000, 23000, 48000, 67000, 83000 }).Name("Unique visitors");
        })
        .CategoryAxis(axis => axis
            .Categories("Jan", "Feb", "Mar", "Apr", "May", "Jun")
            .MajorGridLines(lines => lines.Visible(false))
        )
        .ValueAxis(axis => axis
            .Numeric()
            .Max(140000)
            .Line(line => line.Visible(false))
            .MajorGridLines(lines => lines.Visible(true))
        )
        .Tooltip(tooltip => tooltip
            .Visible(true)
            .Template("#= series.name #: #= value #")
        )
    ) 
    

    【讨论】:

    • 嗨,我不想要折线图,我想用一些线或点图案而不是颜色填充条形图。如果您对此有任何想法,请告诉我?
    • 我误解了您对最终结果的期望,也许您可​​以举个例子并将其添加到您的问题中。
    • 我已将图像添加到我的问题中。我想要这样的东西
    • 您好,您对此有什么想法吗?
    • 我已经修改了标题的显示方式...我想可以使用栏来这样做。查看 kendo 生成的 html 并尝试查看是否存在允许查找 bar 元素的模式。当然,这种修改不是开箱即用的,kendo 也不支持。
    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 2020-10-05
    相关资源
    最近更新 更多