根据出现次数标记样式

时间:2016-01-10 19:26:38

标签: php tags

假设我有一个标签数组:

[
    ['tag' => 'hello', 'count' => 10],
    ['tag' => 'house', 'count' => 8],
    ['tag' => 'horse', 'count' => 7],
    //any number of other tag
    ['tag' => 'alone', 'count' => 1]
]

标签数量未知,但会根据出现次数(count密钥)进行排序。

现在我想将一些样式(字体大小)应用于这些标记,始终基于出现次数。 例如:第一个标签将为30px,最后一个标签为12px。

我该怎么办?由于我不知道标签的数量,情况变得复杂。

我不需要最终的代码,我需要一个想法来做这件事。 谢谢!

3 个答案:

答案 0 :(得分:1)

其中一种方法是 - 获得所有标签public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; FloatingActionButton addLocationFab; Double latitude, longitude; LatLng userCurrentPosition; String coordl1, coordl2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); addLocationFab = (FloatingActionButton) findViewById(R.id.addLocationFAB); } /** * Manipulates the map once available. * This callback is triggered when the map is ready to be used. * This is where we can add markers or lines, add listeners or move the camera. In this case, * we just add a marker near Sydney, Australia. * If Google Play services is not installed on the device, the user will be prompted to install * it inside the SupportMapFragment. This method will only be triggered once the user has * installed Google Play services and returned to the app. */ @Override public void onMapReady(final GoogleMap googleMap) { mMap = googleMap; googleMap.setMyLocationEnabled(true); addLocationFab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { latitude = googleMap.getMyLocation().getLatitude(); longitude = googleMap.getMyLocation().getLongitude(); coordl1 = latitude.toString(); coordl2 = longitude.toString(); Log.d("latitude", coordl1); Log.d("longitude", coordl2); final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { Bundle bundle = new Bundle(); bundle.putString("latitude", coordl1); bundle.putString("longitude", coordl2); PostARequest postARequest = new PostARequest(); postARequest.setArguments(bundle); } }, 5000); } }); } } 的总和。

然后每个标签的重量为count

您可以为最小count/sum(count)值设置最小字体大小并相应地进行缩放,或者将最大字体大小设置为最大count/sum(count)和缩放它失败了。

答案 1 :(得分:1)

最好的算法是:

<?php
    $a = 
    [
        ['tag' => 'hello', 'count' => 10],
        ['tag' => 'house', 'count' => 8],
        ['tag' => 'horse', 'count' => 7],
        //any number of other tag
        ['tag' => 'alone', 'count' => 1]
    ];

    $max = $a[0]['count'];
    foreach($a as $n => $item){

        $max = ($item['count'] > $max) ? $item['count'] : $max;

    }
    foreach($a as $n => $item){

        $a[$n]['font_size'] = 12 + floor(($item['count'] / $max) * 18);

    }
    print_r($a);

结果:

Array
(
    [0] => Array
        (
            [tag] => hello
            [count] => 10
            [font_size] => 30
        )

    [1] => Array
        (
            [tag] => house
            [count] => 8
            [font_size] => 26
        )

    [2] => Array
        (
            [tag] => horse
            [count] => 7
            [font_size] => 24
        )

    [3] => Array
        (
            [tag] => alone
            [count] => 1
            [font_size] => 13
        )

)

`

答案 2 :(得分:0)

这是正确的解决方案。

首先,要将值(int yourValue = TextBoxD1.Text.ParseInt(); )从数字刻度(x)更改为另一个(minX-maxX),这是等式:
minY-maxY

我们假设:

  • y = ((x-minX) / (maxX-minX) * (maxY-minY)) + minY是我们要应用于当前标记的尺寸;
  • size当前标记的出现次数;
  • count是出现次数最多的标记的出现次数;
  • maxTag是出现次数最少的标记的出现次数;
  • minTagmaxFont是我们要使用的最大字体大小。

所以:
minFont y = ((x-minX) / (maxX-minX) * (maxY+minY)) + minY

现在PHP代码非常简单:

size = ((count-minTag) / (maxTag-minTag) * (maxFont-minFont)) + minFont

一个例子。这是 $maxCount = $tags[0]['count']; $minCount = end($tags)['count']; $maxFont = 60; $minFont = 12; foreach($tags as $k => $tag) { $tags[$k]['size'] = round((($tag['count'] - $minCount) / ($maxCount - $minCount) * ($maxFont - $minFont)) + $minFont); }

$tags

结果将是:

[
    (int) 0 => [
        'tag' => 'noombrina',
        'count' => (int) 34
    ],
    (int) 1 => [
        'tag' => 'comunedilanciano',
        'count' => (int) 22
    ],
    (int) 2 => [
        'tag' => 'lucianodalfonso',
        'count' => (int) 21
    ],
    (int) 3 => [
        'tag' => 'comunali2016',
        'count' => (int) 20
    ],
    (int) 4 => [
        'tag' => 'regioneabruzzo',
        'count' => (int) 19
    ],
    (int) 5 => [
        'tag' => 'forzaitalia',
        'count' => (int) 19
    ],
    (int) 6 => [
        'tag' => 'maurofebbo',
        'count' => (int) 17
    ],
    (int) 7 => [
        'tag' => 'mariopupillo',
        'count' => (int) 17
    ],
    (int) 8 => [
        'tag' => 'carabinieri',
        'count' => (int) 13
    ],
    (int) 9 => [
        'tag' => 'toniapaolucci',
        'count' => (int) 13
    ]
]