Exemplo n.º 1
0
        public static void InitRadarTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, Radar radar,
                                            ThemeInfo themeInfo)
        {
            if (!serie.show)
            {
                return;
            }
            if (serie.radarIndex != radar.index)
            {
                return;
            }
            var dataIndex = tooltip.runtimeDataIndex[1];
            var serieData = serie.GetSerieData(dataIndex);

            if (!serieData.show)
            {
                return;
            }
            var numericFormatter = GetItemNumericFormatter(tooltip, serie, serieData);

            switch (serie.radarType)
            {
            case RadarType.Multiple:
                if (radar.isAxisTooltip)
                {
                    var dimension = tooltip.runtimeDataIndex[2];
                    if (!string.IsNullOrEmpty(serie.name))
                    {
                        sb.Append(serie.name).Append("\n");
                    }
                    var total = serie.GetDataTotal(dimension);
                    var first = true;
                    for (int i = 0; i < serie.dataCount; i++)
                    {
                        var sd = serie.GetSerieData(i);
                        if (!sd.show)
                        {
                            continue;
                        }
                        var key           = sd.name;
                        var value         = sd.GetData(dimension);
                        var itemFormatter = GetItemFormatter(tooltip, serie, sd);
                        numericFormatter = GetItemNumericFormatter(tooltip, serie, sd);
                        if (!first)
                        {
                            sb.Append("\n");
                        }
                        first = false;
                        sb.Append("<color=#").Append(themeInfo.GetColorStr(i)).Append(">● </color>");
                        if (string.IsNullOrEmpty(itemFormatter))
                        {
                            if (string.IsNullOrEmpty(key))
                            {
                                key = radar.indicatorList[dataIndex].name;
                            }
                            if (string.IsNullOrEmpty(key))
                            {
                                sb.AppendFormat("{0}\n", ChartCached.FloatToStr(value, numericFormatter));
                            }
                            else
                            {
                                sb.AppendFormat("{0}: {1}\n", key, ChartCached.FloatToStr(value, numericFormatter));
                            }
                        }
                        else
                        {
                            string content = itemFormatter;
                            FormatterHelper.ReplaceSerieLabelContent(ref content, numericFormatter, value, total, serie.name, sd.name);
                            sb.Append(content);
                        }
                    }
                }
                else
                {
                    if (serie.index != tooltip.runtimeDataIndex[0])
                    {
                        return;
                    }
                    sb.Append(serieData.name);
                    for (int i = 0; i < radar.indicatorList.Count; i++)
                    {
                        string key   = radar.indicatorList[i].name;
                        float  value = serieData.GetData(i);
                        if ((i == 0 && !string.IsNullOrEmpty(serieData.name)) || i > 0)
                        {
                            sb.Append(FormatterHelper.PH_NN);
                        }
                        sb.AppendFormat("{0}: {1}", key, ChartCached.FloatToStr(value, numericFormatter));
                    }
                }
                break;

            case RadarType.Single:
                var key2           = serieData.name;
                var value2         = serieData.GetData(1);
                var total2         = serie.GetDataTotal(1);
                var itemFormatter2 = GetItemFormatter(tooltip, serie, serieData);
                if (string.IsNullOrEmpty(itemFormatter2))
                {
                    if (string.IsNullOrEmpty(key2))
                    {
                        key2 = radar.indicatorList[dataIndex].name;
                    }
                    sb.AppendFormat("{0}: {1}", key2, ChartCached.FloatToStr(value2, numericFormatter));
                }
                else
                {
                    string content = itemFormatter2;
                    FormatterHelper.ReplaceSerieLabelContent(ref content, numericFormatter, value2, total2, serie.name, serieData.name);
                    sb.Append(content);
                }

                break;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 替换字符串中的通配符,支持的通配符有{.}、{a}、{b}、{c}、{d}。
        /// </summary>
        /// <param name="content">要替换的字符串</param>
        /// <param name="dataIndex">选中的数据项serieData索引</param>
        /// <param name="numericFormatter">默认的数字格式化</param>
        /// <param name="serie">选中的serie</param>
        /// <param name="series">所有serie</param>
        /// <param name="theme">用来获取指定index的颜色</param>
        /// <param name="category">选中的类目,一般用在折线图和柱状图</param>
        /// <param name="dataZoom">dataZoom</param>
        /// <returns></returns>
        public static bool ReplaceContent(ref string content, int dataIndex, string numericFormatter, Serie serie, Series series,
                                          ChartTheme theme, string category = null, DataZoom dataZoom = null)
        {
            var foundDot = false;
            var mc       = s_Regex.Matches(content);

            foreach (var m in mc)
            {
                var old       = m.ToString();
                var args      = s_RegexSub.Matches(m.ToString());
                var argsCount = args.Count;
                if (argsCount <= 0)
                {
                    continue;
                }
                int  targetIndex = 0;
                char p           = GetSerieIndex(args[0].ToString(), ref targetIndex);
                if (targetIndex >= 0)
                {
                    serie = series.GetSerie(targetIndex);
                    if (serie == null)
                    {
                        continue;
                    }
                }
                else if (serie != null)
                {
                    targetIndex = serie.index;
                }
                else
                {
                    serie       = series.GetSerie(0);
                    targetIndex = 0;
                }
                if (serie == null)
                {
                    continue;
                }
                if (p == '.')
                {
                    var bIndex = targetIndex;
                    if (argsCount >= 2)
                    {
                        var args1Str = args[1].ToString();
                        if (s_RegexN.IsMatch(args1Str))
                        {
                            bIndex = int.Parse(args1Str);
                        }
                    }
                    content  = content.Replace(old, ChartCached.ColorToDotStr(theme.GetColor(bIndex)));
                    foundDot = true;
                }
                else if (p == 'a' || p == 'A')
                {
                    if (argsCount == 1)
                    {
                        content = content.Replace(old, serie.name);
                    }
                }
                else if (p == 'b' || p == 'B')
                {
                    var bIndex = dataIndex;
                    if (argsCount >= 2)
                    {
                        var args1Str = args[1].ToString();
                        if (s_RegexN.IsMatch(args1Str))
                        {
                            bIndex = int.Parse(args1Str);
                        }
                    }
                    var needCategory = serie.type == SerieType.Line || serie.type == SerieType.Bar;
                    if (needCategory)
                    {
                        content = content.Replace(old, category);
                    }
                    else
                    {
                        var serieData = serie.GetSerieData(bIndex, dataZoom);
                        content = content.Replace(old, serieData.name);
                    }
                }
                else if (p == 'c' || p == 'C' || p == 'd' || p == 'D')
                {
                    var isPercent      = p == 'd' || p == 'D';
                    var bIndex         = dataIndex;
                    var dimensionIndex = -1;
                    if (argsCount >= 2)
                    {
                        var args1Str = args[1].ToString();
                        if (s_RegexFn.IsMatch(args1Str))
                        {
                            numericFormatter = args1Str;
                        }
                        else if (s_RegexN_N.IsMatch(args1Str))
                        {
                            var temp = args1Str.Split('-');
                            bIndex         = int.Parse(temp[0]);
                            dimensionIndex = int.Parse(temp[1]);
                        }
                        else if (s_RegexN.IsMatch(args1Str))
                        {
                            dimensionIndex = int.Parse(args1Str);
                        }
                        else
                        {
                            Debug.LogError("unmatch:" + args1Str);
                            continue;
                        }
                    }
                    if (argsCount >= 3)
                    {
                        numericFormatter = args[2].ToString();
                    }
                    if (dimensionIndex == -1)
                    {
                        dimensionIndex = 1;
                    }
                    if (numericFormatter == string.Empty)
                    {
                        numericFormatter = SerieHelper.GetNumericFormatter(serie, serie.GetSerieData(bIndex));
                    }
                    var value = serie.GetData(bIndex, dimensionIndex, dataZoom);
                    if (isPercent)
                    {
                        var total   = serie.GetDataTotal(dimensionIndex);
                        var percent = total == 0 ? 0 : value / serie.yTotal * 100;
                        content = content.Replace(old, ChartCached.FloatToStr(percent, numericFormatter));
                    }
                    else
                    {
                        content = content.Replace(old, ChartCached.FloatToStr(value, numericFormatter));
                    }
                }
            }
            content = s_RegexNewLine.Replace(content, PH_NN);
            return(foundDot);
        }