简体   繁体   English

在KendoUI饼图上设置工具提示标签颜色

[英]Setting Tool tip label colors on a KendoUI Pie Chart

I am using a .kendoChart() call to create my self a pie chart. 我正在使用.kendoChart()调用来创建一个饼图。

seriesColors: config.colors,
tooltip: {
    visible: true,
    template: function (e) {
        return shared.AssetClassName(e.category) + ' ' + shared.toString(e.percentage, "p0");
    }
}

Using seriesColors: config.colors I am overriding the normal color set that comes with Kendo UI. 使用seriesColors: config.colors我覆盖了Kendo UI附带的普通颜色集。 The problem with this is when the chart uses darker colors the label color in the tooltip on hover is always black and is very difficult to read. 这个问题是当图表使用较暗的颜色时,悬停时工具提示中的标签颜色总是黑色并且很难读取。 I am looking for a way to reference another color array, set the colors on bind or something similar to that. 我正在寻找一种方法来引用另一个颜色数组,设置绑定上的颜色或类似的颜色。

Kendo UI handles the dark colors in the standard color set by changing the label colors to white automatically so there should be a way to do it. Kendo UI通过将标签颜色自动更改为白色来处理标准颜色集中的深色,因此应该有办法实现。

I have done some research but I cannot find a good set of documentation for Kendo UI similar to what Microsoft usually releases. 我做了一些研究,但是我找不到类似于微软通常发布的Kendo UI的一套好的文档。

Update: 更新:

Joe's response was very helpful but it did not quite get me there. Joe的回复非常有帮助,但它并没有让我在那里。

Using the Color: attribute I can indeed set the ToolTip text color on a global scale, but... what if I have a light yellow? 使用Color:属性我确实可以在全局范围内设置ToolTip文本颜色,但是...如果我有浅黄色怎么办? Is there a way to specify directly what color the text should be on what background color? 有没有办法直接指定文本应该是什么颜色的背景颜色?

Will Color: accept a function{} or array of colors somehow? 将Color:以某种方式接受函数{}或颜色数组?

Thanks, 谢谢,


Thanks to Roc for showing me exactly what I was missing! 感谢Roc向我展示了我错过的东西!

Note: I used 120 luma for my determining value of if I would use black or white. 注意:如果我使用黑色或白色,我使用120 luma作为我的确定值。

You can set this via the tooltip options (code below is from their dojo) right at the bottom I set the tooltips to #ff0000 . 您可以通过工具提示选项(下面的代码来自他们的dojo)在底部设置,我将工具提示设置为#ff0000

The documentation is pretty solid (if a little awkward to navigate) 文档非常可靠(如果导航有点尴尬)

http://docs.telerik.com/kendo-ui/api/dataviz/chart#configuration-tooltip.background http://docs.telerik.com/kendo-ui/api/dataviz/chart#configuration-tooltip.background

        $("#chart").kendoChart({
            title: {
                position: "bottom",
                text: "Share of Internet Population Growth, 2007 - 2012"
            },
            legend: {
                visible: false
            },
            chartArea: {
                background: ""
            },
            seriesDefaults: {
                labels: {
                    visible: true,
                    background: "transparent",
                    template: "#= category #: \n #= value#%"
                }
            },
            series: [{
                type: "pie",
                startAngle: 150,
                data: [{
                    category: "Asia",
                    value: 53.8,
                    color: "#9de219"
                },{
                    category: "Europe",
                    value: 16.1,
                    color: "#90cc38"
                },{
                    category: "Latin America",
                    value: 11.3,
                    color: "#068c35"
                },{
                    category: "Africa",
                    value: 9.6,
                    color: "#006634"
                },{
                    category: "Middle East",
                    value: 5.2,
                    color: "#004d38"
                },{
                    category: "North America",
                    value: 3.6,
                    color: "#033939"
                }]
            }],
            tooltip: {
                visible: true,
                format: "{0}%",
              background: "#ff0000"
            }
        });

You were very close to the solution in your question since you can use a function delegate as a template. 您非常接近问题中的解决方案,因为您可以使用函数委托作为模板。 Kendo tooltip is a div element, so just return an html block with the color you need and the tooltips will be white text on the dark background or a black text on the light background. 剑道工具提示是一个div元素,所以只需返回一个具有所需颜色的html块,工具提示将是深色背景上的白色文本或浅色背景上的黑色文本。

To detect if the background is too dark you can use the following thread How to check if hex color is "too black"? 要检测背景是否太暗,可以使用以下线程如何检查十六进制颜色是否“太黑”? agains the series color from the "e" object - e.series.color. 再次来自“e”对象的系列颜色 - e.series.color。 I used an abstract function getColorLuma() below to avoid duplication. 我在下面使用了一个抽象函数getColorLuma()来避免重复。

seriesColors: config.colors,
tooltip: {
    visible: true,
    template: function (e) {
        var textColor = getColorLuma(e.series.color) < 128 ? 'white' : 'black';
        return '<span style="color:' + textColor + '">' +  
          shared.AssetClassName(e.category) + ' ' + shared.toString(e.percentage, "p0") +
          '</span>';
    }
}

But be careful with the using ' and # in the text returned from the template function. 但要小心在模板函数返回的文本中使用'和#。 Javascript will crash. Javascript会崩溃。 I just used 'white' and 'black' in my code instead of hex colors. 我在代码中使用了“白色”和“黑色”而不是十六进制颜色。

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

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